本文主要是介绍DrawerLayout(侧拉)与多条目的展示,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//创建侧拉listview的条目
<resources><string name="app_name">RIKAO0912</string><string-array name="news"><item>关注</item><item>推荐</item><item>热点</item><item>视频</item><item>数码</item><item>科技</item></string-array>
</resources>
//布局
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="match_parent"android:orientation="vertical"><ListViewandroid:layout_width="wrap_content"android:layout_height="match_parent"android:id="@+id/listview"/></LinearLayout><ListViewandroid:layout_width="180sp"android:layout_height="match_parent"android:layout_gravity="left"android:id="@+id/lv"android:entries="@array/news"android:background="#fff"/></android.support.v4.widget.DrawerLayout>
//具体的方法操作
import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast;import com.google.gson.Gson;import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.List;public class MainActivity extends AppCompatActivity {private DrawerLayout dl;private ListView lv1,lv2;private static final String surl="http://v.juhe.cn/toutiao/index?type=top&key=2f092bd9ce76c0257052d6d3c93c11b4http://v.juhe.cn/toutiao/index?type=";String ss="&key=2f092bd9ce76c0257052d6d3c93c11b4";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);dl=(DrawerLayout)findViewById(R.id.activity_main);lv1=(ListView)findViewById(R.id.lv);lv2=(ListView)findViewById(R.id.listview);dl.openDrawer(lv1);//侧拉的条目监听lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {Toast.makeText(MainActivity.this,"qqqq"+i,Toast.LENGTH_SHORT).show();if(i==0){int p=1;init(surl+p+ss);}elseif(i==1){int p=2;init(surl+p+ss);}elseif(i==2){int p=3;init(surl+p+ss);}elseif(i==3){int p=4;init(surl+p+ss);}}});}private void init(String f) {new AsyncTask<String,Integer,String>(){@Overrideprotected String doInBackground(String... strings) {String ss=strings[0];String str="";try {URL url = new URL(ss);HttpURLConnection http=(HttpURLConnection)url.openConnection();http.setReadTimeout(2000);http.setConnectTimeout(2000);int rc = http.getResponseCode();if(rc==200){InputStream in = http.getInputStream();byte[] b=new byte[1024];int len=0;while((len=in.read(b))!=-1){str+=new String(b,0,len);}}} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return str;}@Overrideprotected void onPostExecute(String s) {super.onPostExecute(s);Log.i("TAG",s);List<JavaBean.ResultBean.DataBean> data = new Gson().fromJson(s, JavaBean.class).getResult().getData();Log.i("TAG",data.size()+"");//设置适配器lv2.setAdapter(new Myadapter(data,MainActivity.this));}}.execute(f);} }
//创建universal_Imager_Loader,注意导入gson和universal_Imager_Loader的架包import android.app.Application;import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;public class MyApplication extends Application {@Overridepublic void onCreate() {super.onCreate();ImageLoaderConfiguration con=new ImageLoaderConfiguration.Builder(this).build();ImageLoader.getInstance().init(con);}}//创建适配器的类package com.example.shenxuesong.rikao0912;import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView;import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.assist.ImageSize;import java.util.List;public class Myadapter extends BaseAdapter {private List<JavaBean.ResultBean.DataBean> data;private Context context;public Myadapter(List<JavaBean.ResultBean.DataBean> data, Context context) {this.data = data;this.context = context;}@Overridepublic int getCount() {return data.size();}@Overridepublic Object getItem(int i) {return data.get(i);}@Overridepublic long getItemId(int i) {return i;}//判断子条目的布局@Overridepublic int getItemViewType(int position) {int re=0;if(data.get(position).getThumbnail_pic_s()!=null&&data.get(position).getThumbnail_pic_s02()!=null&&data.get(position).getThumbnail_pic_s03()!=null){re=0;}else if(data.get(position).getThumbnail_pic_s()!=null&&data.get(position).getThumbnail_pic_s02()!=null&&data.get(position).getThumbnail_pic_s03()==null){re=1;}else if(data.get(position).getThumbnail_pic_s()!=null&&data.get(position).getThumbnail_pic_s02()==null&&data.get(position).getThumbnail_pic_s03()==null){re=2;}return re;}@Overridepublic int getViewTypeCount() {return 3;}@Overridepublic View getView(int i, View view, ViewGroup viewGroup) {ViewHolder1 holder1=null;ViewHolder2 holder2=null;ViewHolder3 holder3=null;int re = getItemViewType(i);if(re==0){if(view==null){view=View.inflate(context,R.layout.item1,null);holder1=new ViewHolder1();holder1.tv=(TextView) view.findViewById(R.id.tv);holder1.image1=(ImageView)view.findViewById(R.id.image1);holder1.image2=(ImageView)view.findViewById(R.id.image2);holder1.image3=(ImageView)view.findViewById(R.id.image3);view.setTag(holder1);}else{holder1= (ViewHolder1) view.getTag();}holder1.tv.setText(data.get(i).getTitle()+"\n"+data.get(i).getAuthor_name()+"\n"+data.get(i).getDate());String thumbnail_pic_s = data.get(i).getThumbnail_pic_s();String thumbnail_pic_s02 = data.get(i).getThumbnail_pic_s02();String thumbnail_pic_s03 = data.get(i).getThumbnail_pic_s03();ImageSize size=new ImageSize(100,100);ImageLoader.getInstance().displayImage(thumbnail_pic_s,holder1.image1,size);ImageLoader.getInstance().displayImage(thumbnail_pic_s02,holder1.image2,size);ImageLoader.getInstance().displayImage(thumbnail_pic_s03,holder1.image3,size);}else if(re==1){if(view==null){view=View.inflate(context,R.layout.item2,null);holder2=new ViewHolder2();holder2.tv=(TextView) view.findViewById(R.id.tv);holder2.image1=(ImageView)view.findViewById(R.id.image1);holder2.image2=(ImageView)view.findViewById(R.id.image2);view.setTag(holder2);}else{holder2= (ViewHolder2) view.getTag();}holder2.tv.setText(data.get(i).getTitle()+"\n"+data.get(i).getAuthor_name()+"\n"+data.get(i).getDate());String thumbnail_pic_s = data.get(i).getThumbnail_pic_s();String thumbnail_pic_s02 = data.get(i).getThumbnail_pic_s02();ImageSize size=new ImageSize(100,100);ImageLoader.getInstance().displayImage(thumbnail_pic_s,holder2.image1,size);ImageLoader.getInstance().displayImage(thumbnail_pic_s02,holder2.image2,size);}else if(re==2){if(view==null){view=View.inflate(context,R.layout.item3,null);holder3=new ViewHolder3();holder3.tv=(TextView) view.findViewById(R.id.tv);holder3.image1=(ImageView)view.findViewById(R.id.image1);view.setTag(holder3);}else{holder3= (ViewHolder3) view.getTag();}holder3.tv.setText(data.get(i).getTitle()+"\n"+data.get(i).getAuthor_name()+"\n"+data.get(i).getDate());String thumbnail_pic_s = data.get(i).getThumbnail_pic_s();;ImageSize size=new ImageSize(100,100);ImageLoader.getInstance().displayImage(thumbnail_pic_s,holder3.image1,size);}return view;}class ViewHolder3{TextView tv;ImageView image1;}class ViewHolder2{TextView tv;ImageView image1;ImageView image2;}class ViewHolder1{TextView tv;ImageView image1;ImageView image2;ImageView image3;} }//配置权限<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.shenxuesong.rikao0912"> <uses-permission android:name="android.permission.INTERNET"/><applicationandroid:name=".MyApplication"android:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/Theme.AppCompat.Light"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>
这篇关于DrawerLayout(侧拉)与多条目的展示的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!