package com.example.demo2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class Fragment1 extends Fragment{
private ListView mListview;
private SimpleAdapter mSAdapter;
private int[] image;
private String[] news_title;
private String[] news_time;
private String[] news_introduce;
private List<Map<String, Object>> mList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.view1, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
mListview =(ListView) getActivity().findViewById(R.id.listview1);
setData();
mListview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity(), DetailsActivity.class);
startActivity(intent);
}
});
}
private void setData() {
mList= new ArrayList<Map<String,Object>>();
image = new int[]{R.drawable.r1,R.drawable.r2,R.drawable.r3,R.drawable.r4};
news_title= new String[]{"标题1","标题2","标题3","标题4"};
news_time= new String[]{"2016-12-21","2016-12-21","2016-12-21","2016-12-21"};
news_introduce= new String[]{"内容1","内容2","内容","内容4"};
mSAdapter = new SimpleAdapter(getActivity(), getdata(), R.layout.item,
new String[] { "pic", "title", "time", "introduce" },
new int[] { R.id.news_image, R.id.news_title, R.id.news_time, R.id.news_introduce });
mListview.setAdapter(mSAdapter);
}
private List<? extends Map<String, Object>> getdata() {
for(int i=0;i<image.length;i++){
Map<String, Object> map = new HashMap<String, Object>();
map.put("pic", image[i]);
map.put("title", news_title[i]);
map.put("time", news_time[i]);
map.put("introduce", news_introduce[i]);
mList.add(map);
}
return mList;
}
}
package com.example.demo2;
import android.app.Activity;
import android.os.Bundle;
public class DetailsActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.details_layout);
}
}
package com.example.demo2;
import java.util.ArrayList;
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends FragmentActivity {
private List<Fragment> mList;
private PagerTabStrip mTab;
private ViewPager mViewPager;
private List<String> mTitleList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager =(ViewPager) findViewById(R.id.parger);
add();
}
private void add() {
mList = new ArrayList<Fragment>();
mList.add(new Fragment1());
mList.add(new Fragment2());
mList.add(new Fragment3());
mList.add(new Fragment4());
mTitleList = new ArrayList<String>();
mTitleList.add("热点");
mTitleList.add("推荐");
mTitleList.add("社会");
mTitleList.add("军事");
mTab=(PagerTabStrip)findViewById(R.id.tab);
mTab.setBackgroundColor(Color.GRAY);
mTab.setTextColor(Color.WHITE);
mTab.setDrawFullUnderline(false);
mTab.setTabIndicatorColor(Color.RED);
MyFragmentPagerAdapter adapter =new MyFragmentPagerAdapter(getSupportFragmentManager(), mList, mTitleList);
mViewPager.setAdapter(adapter);
}
}
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
private List<Fragment> mList;
private List<String> mTitleList;
public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> mList, List<String> mTitleList) {
super(fm);
this.mList=mList;
this.mTitleList=mTitleList;
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return mList.get(arg0);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mList.size();
}
@Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return mTitleList.get(position);
}
}
}
package com.example.demo2;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.view2, container, false);
}
}
package com.example.demo2;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment3 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.view3, container, false);
}
}
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment4 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.view4, container, false);
}
}
我是小hang家
相关分类