我正在努力实现Airbnb app for Android的效果,列出每行中水平滚动的图库(每行都有顶层,标题为so)。我在每个单元格中使用ListView
FrameLayout
和ViewPager
,但滚动时我得到NullPointerException
。带有图片的Fragments
会丢失指向父片段的指针(我发现Android Support Library
中有一些错误,我花了几天时间寻找解决方案)。
现在我不想显示代码。我只想知道Viewpager
ListView
是个好主意,或者我必须以另一种方式来做。也许有一个库可以实现这种行为?
答案 0 :(得分:1)
我在此页http://dorkus.hatenablog.com/entry/2014/01/16/021853找到了解决方案。需要一些优化,但是它是未来工作的良好基础。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="center" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical" >
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#000000"
android:singleLine="true"
android:textSize="20sp" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00000000" >
</RelativeLayout>
<Button
android:id="@+id/button2"
android:layout_width="70dp"
android:layout_height="match_parent"
android:background="#cc2828"
android:text="削除"
android:textColor="#ffffff" />
</LinearLayout>
public class MyListAdapter extends ArrayAdapter<String>{
private LayoutInflater inflater = null;
private static final float BUTTON_WIDTH_DP = 70f;
private int margin;
public MyListAdapter(Context context, int resource,String[] items) {
super(context, resource,items);
inflater = LayoutInflater.from(context);
//ページ2のRelativeLayoutの幅を計算してmarginへ格納する。
float density = getContext().getResources().getDisplayMetrics().density;
int buttonWidthPX = (int) (BUTTON_WIDTH_DP * density + 0.5f);
WindowManager wm = (WindowManager)getContext().getSystemService(getContext().WINDOW_SERVICE);
Display dp = wm.getDefaultDisplay();
margin = dp.getWidth() - buttonWidthPX;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = inflater.inflate(R.layout.row,null);
}
ViewPager viewPager = (ViewPager)convertView.findViewById(R.id.viewpager);
viewPager.setPageMargin(-margin);
MyPagerAdapter adapter = new MyPagerAdapter(getContext(),getItem(position));
viewPager.setAdapter(adapter);
return convertView;
}
}
public class MyPagerAdapter extends PagerAdapter{
private LayoutInflater inflater;
private static final int PAGE_NUM = 2;
private String str;
public MyPagerAdapter(Context context,String str) {
super();
inflater = LayoutInflater.from(context);
this.str = str;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
LinearLayout layout = null;
if(position == 0){
layout = (LinearLayout)inflater.inflate(R.layout.page1, null);
TextView text = (TextView)layout.findViewById(R.id.text);
text.setText(str);
}else{
layout = (LinearLayout)inflater.inflate(R.layout.page2, null);
}
container.addView(layout);
return layout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
@Override
public int getCount() {
return PAGE_NUM;
}
@Override
public boolean isViewFromObject(View view, Object obj) {
return view.equals(obj);
}
}
public class MainActivity extends Activity {
private String str[] = {"項目1","項目2","項目3","項目4","項目5","項目6","項目7","項目8","項目9","項目10"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView)findViewById(R.id.listView);
MyListAdapter adapter = new MyListAdapter(this,R.layout.row,str);
listView.setAdapter(adapter);
}
}