gpt4 book ai didi

ListView 行中的 Android 图库

转载 作者:行者123 更新时间:2023-11-29 01:46:07 25 4
gpt4 key购买 nike

我正在尝试实现诸如适用于 Android 的 Airbnb 应用程序的效果,列出每行中水平滚动画廊的位置(并且每行都有带标题的顶层)。我在每个单元格中使用带有 FrameLayoutViewPagerListView,但在滚动时出现 NullPointerException。带有图片的 Fragments 丢失了指向父 fragment 的指针(我发现 Android Support Library 有一些错误,我花了几天时间寻找解决方案)。

现在我不想展示代码。我只是想知道 ListView 中的 Viewpager 是个好主意,还是我必须以另一种方式来做。也许有一个库可以实现这种行为?

最佳答案

我在此页面上找到了解决方案 http://dorkus.hatenablog.com/entry/2014/01/16/021853 .需要一些优化,但恕我直言,它是 future 工作的良好基础。

编辑

View in action

List Item Layout

Item in List

主.xml

<?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

<?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>

page1.xml

<?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>

page2.xml

<?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>

MyListAdapter.java

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;
}
}

MyPagerAdapter.java

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);
}

}

主 Activity .java

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);
}
}

关于ListView 行中的 Android 图库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21380540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com