///////////////////////////////布局文件 |
<?xml version= "1.0" encoding= "utf-8" ?> |
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" |
xmlns:tools= "http://schemas.android.com/tools" |
xmlns:app= "http://schemas.android.com/apk/res-auto" |
android:id= "@+id/activity_main" |
android:layout_width= "match_parent" |
android:layout_height= "match_parent" |
android:orientation= "vertical" |
tools:context= "com.bwie.xujiahao20170213.MainActivity" > |
<android.support.design.widget.TabLayout |
android:id= "@+id/tablayout" |
android:layout_width= "match_parent" |
android:layout_height= "wrap_content" |
android:background= "@color/backgroundColor" |
app:tabIndicatorColor= "@color/tabIndicatorColor" |
app:tabTextColor= "@color/TextColor" |
app:tabSelectedTextColor= "@color/colorAccent" |
app:tabMode= "scrollable" |
></android.support.design.widget.TabLayout> |
<View |
android:layout_width= "match_parent" |
android:layout_height= "1dp" |
android:background= "#ffF456" |
></View> |
<android.support.v4.view.ViewPager |
android:id= "@+id/viewpager" |
android:layout_width= "match_parent" |
android:layout_height= "match_parent" |
></android.support.v4.view.ViewPager> |
</LinearLayout> |
<?xml version= "1.0" encoding= "utf-8" ?> |
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" |
android:orientation= "vertical" android:layout_width= "match_parent" |
android:layout_height= "match_parent" > |
<ListView |
android:id= "@+id/listview" |
android:layout_width= "wrap_content" |
android:layout_height= "wrap_content" |
></ListView> |
</LinearLayout> |
<?xml version= "1.0" encoding= "utf-8" ?> |
<RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android" |
android:layout_width= "match_parent" |
android:layout_height= "match_parent" > |
<ImageView |
android:layout_width= "140dp" |
android:layout_height= "130dp" |
android:id= "@+id/img" |
android:src= "@mipmap/ic_launcher" /> |
<TextView |
android:layout_width= "wrap_content" |
android:layout_height= "wrap_content" |
android:id= "@+id/title2" |
android:text= "TextView" |
android:layout_toRightOf= "@+id/img" |
android:textSize= "20dp" /> |
<TextView |
android:layout_width= "wrap_content" |
android:layout_height= "wrap_content" |
android:id= "@+id/wang2" |
android:text= "TextView" |
android:layout_toRightOf= "@+id/img" |
android:textSize= "20dp" |
android:layout_below= "@+id/title2" |
android:layout_marginTop= "15dp" /> |
</RelativeLayout> |
、、、、、、、、、、、、、主要代码MainActivity |
public class MainActivity extends AppCompatActivity { |
private TabLayout tabLayout; |
private ViewPager viewPager; |
private String[] title= new String[]{ "头条" , "社会" , "国内" , "国际" , "娱乐" , "体育" , "军事" , "游戏" , "女性" }; |
private List<Fragment> list; |
@Override |
protected void onCreate(Bundle savedInstanceState) { |
super .onCreate(savedInstanceState); |
setContentView(R.layout.activity_main); |
//初始化控件 |
tabLayout = (TabLayout) findViewById(R.id.tablayout); |
viewPager = (ViewPager) findViewById(R.id.viewpager); |
initData(); |
//设置适配器 |
MyAdapter adapter = new MyAdapter(getSupportFragmentManager(), MainActivity. this , title); |
adapter.setList(list); |
//关联viewpager和tablayout |
viewPager.setAdapter(adapter); |
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); |
tabLayout.setupWithViewPager(viewPager); |
} |
//根据数组添加fragment界面 |
private void initData() { |
list = new ArrayList<>(); |
for ( int i= 0 ;i<title.length;i++){ |
TitleFragment fragment = new TitleFragment(); |
list.add(fragment); |
} |
} |
} |
public class MyApplication extends Application{ |
@Override |
public void onCreate() { |
super .onCreate(); |
ImageLoaderConfiguration configuration= new ImageLoaderConfiguration.Builder( this ).build(); |
ImageLoader.getInstance().init(configuration); |
} |
} |
public class MyAdapter extends FragmentPagerAdapter{ |
private List<Fragment> list; |
private Context context; |
private String[] title; |
public MyAdapter(FragmentManager fm,Context context,String[] title) { |
super (fm); |
this .context=context; |
this .title=title; |
} |
public void setList(List<Fragment> list) { |
this .list = list; |
} |
@Override |
public CharSequence getPageTitle( int position) { |
return title[position]; |
} |
@Override |
public Fragment getItem( int position) { |
return list.get(position); |
} |
@Override |
public int getCount() { |
return list.size(); |
} |
} |
public class MyBase extends BaseAdapter { |
private Context context; |
private List<Daa> list; |
public MyBase(Context context, List<Daa> list) { |
this .context = context; |
this .list = list; |
} |
@Override |
public int getCount() { |
return list.size(); |
} |
@Override |
public Object getItem( int i) { |
return list.get(i); |
} |
@Override |
public long getItemId( int i) { |
return i; |
} |
@Override |
public View getView( int i, View view, ViewGroup viewGroup) { |
ViewHolder viewHolder; |
if (view== null ){ |
viewHolder = new ViewHolder(); |
view=View.inflate(context, R.layout.lv_layout, null ); |
viewHolder.img = (ImageView) view.findViewById(R.id.img); |
viewHolder.title2 = (TextView) view.findViewById(R.id.title2); |
viewHolder.wang2 = (TextView) view.findViewById(R.id.wang2); |
view.setTag(viewHolder); |
} else { |
viewHolder= (ViewHolder) view.getTag(); |
} |
Daa dd=list.get(i); |
//设置缓存图片 |
DisplayImageOptions options= new DisplayImageOptions.Builder() |
.cacheOnDisk( true ) |
.cacheInMemory( true ).build(); |
viewHolder.title2.setText(dd.title); |
viewHolder.wang2.setText(dd.author_name); |
ImageLoader.getInstance().displayImage(dd.thumbnail_pic_s,viewHolder.img,options); |
return view; |
} |
class ViewHolder{ |
public ImageView img; |
public TextView title2; |
public TextView wang2; |
} |
} |
public class StreamUtils { |
public static String parser(InputStream inputStream){ |
StringBuilder sbb= new StringBuilder(); |
String str; |
BufferedReader br= new BufferedReader( new InputStreamReader(inputStream)); |
try { |
while ((str=br.readLine())!= null ){ |
sbb.append(str); |
} |
} catch (IOException e) { |
e.printStackTrace(); |
} |
return sbb.toString(); |
} |
} |
public class TitleFragment extends Fragment { |
private ListView lv; |
@Nullable |
@Override |
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { |
View view1=View.inflate(getActivity(), R.layout.fragment, null ); |
//初始化控件 |
lv = (ListView) view1.findViewById(R.id.listview); |
//设置地址 |
String path= "http://v.juhe.cn/toutiao/index?type=%22+mParam2+%22&key=ebe6062b9049b169b8a611b5051c41c9" ; |
//实例化AsuyncTask |
MyTask myTask = new MyTask(); |
myTask.execute(path); |
return view1; |
} |
class MyTask extends AsyncTask<String,Void,String>{ |
private String result; |
private List<Daa> list; |
@Override |
protected String doInBackground(String... strings) { |
try { |
//请求数据 |
URL url= new URL(strings[ 0 ]); |
HttpURLConnection connection= (HttpURLConnection) url.openConnection(); |
int code=connection.getResponseCode(); |
if (code== 200 ){ |
InputStream inputStream=connection.getInputStream(); |
result = StreamUtils.parser(inputStream); |
} |
//开始解析数据 |
Gson gson= new Gson(); |
JsonBean jsonBean = gson.fromJson(result, JsonBean. class ); |
list = new ArrayList<>(); |
//循环解析 |
for ( int i= 0 ;i<jsonBean.result.data.size();i++){ |
Daa da= new Daa(jsonBean.result.data.get(i).author_name, |
jsonBean.result.data.get(i).thumbnail_pic_s, |
jsonBean.result.data.get(i).title); |
list.add(da); |
} |
} catch (Exception e) { |
e.printStackTrace(); |
} |
return null ; |
} |
@Override |
protected void onPostExecute(String s) { |
super .onPostExecute(s); |
MyBase myBase = new MyBase(getActivity(), list); |
lv.setAdapter(myBase); |
} |
} |
} |
by: 发表于:2017-09-27 10:01:11 顶(0) | 踩(0) 回复
??
回复评论