ViewPager
のリストを作成しようとしています。私は2 Adapter
- ListView
のためにもう一つはViewPagerのために書きました。しかし、私は空リストを参照してくださいenter image description here。私はここに同じ方法で実装してみました:ここ Placing ViewPager as a row in ListView、 View Pager in listview? は私のレイアウトは以下のとおりです。ViewPagerのリストビューを作成する
LISTVIEWレイアウト
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="team14.cs442.com.dummytest.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ListView"
android:background="#e88585">
</ListView>
</RelativeLayout>
PagerView:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pager"
android:longClickable="true"
xmlns:android="http://schemas.android.com/apk/res/android">
</android.support.v4.view.ViewPager>
[表示] - 1:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/image01"
android:src="@android:drawable/divider_horizontal_dark"
android:text="Hello"
xmlns:android="http://schemas.android.com/apk/res/android" />
ビュー-2:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/image02"
android:src="@android:drawable/divider_horizontal_dark"
android:text="Goodbye"
xmlns:android="http://schemas.android.com/apk/res/android" />
コード一覧アダプタ:
class ListViewAdapter extends ArrayAdapter {
Context context;
int layoutId;
public ListViewAdapter(Context context, int resource) {
super(context,R.layout.pagerview, list);
this.context = context;
this.layoutId = resource;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.pagerview, null);}
ViewPager viewPager = (ViewPager) convertView;
viewPager.setAdapter(new CustomAdapter(MainActivity.this));
return convertView;
}
}
ViewPager adapter:
public class MainActivity extends AppCompatActivity implements
ItemFragment.OnListFragmentInteractionListener
{
int[] layouts = {R.layout.image01, R.layout.image02};
ArrayList<String> list = new ArrayList<String>();
ListViewAdapter adapter;
public class CustomAdapter extends PagerAdapter{
Context mContext;
public CustomAdapter(Context context){
mContext = context;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) container.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(layouts[position],null);
container.addView(view, 0);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
@Override
public int getCount() {
return 2;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
@Override
public void finishUpdate(ViewGroup container) {
super.finishUpdate(container);
adapter.notifyDataSetChanged();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
ListView listView = (ListView) findViewById(R.id.ListView);
adapter = new ListViewAdapter(this, R.layout.pagerview);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
私はここで何をしないのですか?
あなたのonCreateで以下のようにアダプタを設定しますあなたのListViewに値を設定していないようです。 – NSimon