想在View中弄个LIstVIew,直接将View还是当作布局文件操作是否可以?
是 新建一个类让它去实现ListView要做的就行了,然后再将布局文件显示是这个View就可以了???
求教,谢谢!!
是 新建一个类让它去实现ListView要做的就行了,然后再将布局文件显示是这个View就可以了???
求教,谢谢!!
2016-08-25
import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; import hison.myapplication.R; import hison.myapplication.vp_adapter.MoocQuestionAdapter; import hison.myapplication.vp_adapter.MyViewPagerAdapter; import hison.myapplication.vp_adapter.QuestionAdapter; /** * Created by Administrator on 8-24-0024. */ public class MoocQuestionActivity extends Activity { private Context context; private ViewPager viewPager; private MyListAdapter listviewAdapter; private MyPagerAdapter pageAdapter; private List<String> list; private List<ListView> list_listview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.question_activity); init(); } private void init(){ context = this; viewPager = (ViewPager) findViewById(R.id.vp_question); list = new ArrayList<>(); int i =0; while(i < 10){ list.add("item"+i); i++; } //viewpager里有两个页面,这是第一个页面的listview ListView listView = new ListView(this); listviewAdapter = new MyListAdapter(list,context); listView.setAdapter(listviewAdapter); //第二个页面的listview ListView listView1 = new ListView(this); listView1.setAdapter(listviewAdapter); //初始化list<ListView> 把上面的listview添加进去,作为pageAdapter的参数 list_listview = new ArrayList<>(); list_listview.add(listView); list_listview.add(listView1); //pageAdapter适配器 pageAdapter = new MyPagerAdapter(list_listview); viewPager.setAdapter(pageAdapter); } } //viewpage的适配器 class MyPagerAdapter extends PagerAdapter { private List<ListView> listViews; public MyPagerAdapter(List<ListView> listViews) { this.listViews = listViews; } @Override public int getCount() { return listViews.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub container.addView(listViews.get(position)); return listViews.get(position); } } //listview的适配器 class MyListAdapter extends BaseAdapter { private List<String> list; private Context context; public MyListAdapter(List<String> list, Context context){ this.list = list; this.context = context; } @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) { if(view==null){ ViewHolder viewHolder = new ViewHolder(); view = View.inflate(context, R.layout.item_question,null); viewHolder.textView = (TextView) view.findViewById(R.id.tv_question); viewHolder.textView.setText(list.get(i)); view.setTag(viewHolder); }else{ ViewHolder viewHolder = (ViewHolder) view.getTag(); viewHolder.textView.setText(list.get(i)); } return view; } class ViewHolder{ TextView textView; } } xml文件 <?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"> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/vp_question"> </android.support.v4.view.ViewPager> </LinearLayout>
举报