3

试图ListViewRecyclerView. 不确定是否有人也有使用问题RecyclerView。我注意到在不更改视图项布局的情况下,使用RecyclerView滚动不如使用时平滑ListView,并且对于我的视图项布局仅GridLayoutManager适用。

有什么建议吗?谢谢!

视图项布局未更改,并且一直正常工作ListView(具有 > 1000 个数据)。

ListView

   <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>


public class UsingListViewAdapter extends BaseAdapter;

UsingListViewAdapter mAdaptor new UsingListViewAdapter(null, mSelectListener);
    mListView = (ListView) view.findViewById(R.id.list_view);
    mListView.setAdapter(mAdaptor);

RecyclerView

<android.support.v7.widget.RecyclerView
        android:id="@+id/list_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent”/>

public class UsingRecyclerViewAdapter extends RecyclerView.Adapter< UsingRecyclerViewAdapter.RowViewHolder>
                                       implements View.OnClickListener, View.OnLongClickListener;


UsingRecyclerViewAdapter mAdaptor new UsingRecyclerViewAdapter(null, mSelectListener)
    mRecyclerView = (RecyclerView) view.findViewById(R.id.list_view);
    mRecyclerView.setHasFixedSize(true);

    mLayoutManager = new GridLayoutManager(getActivity(), 1); 
    //mLayoutManager = new LinearLayoutManager(getActivity());  

    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setAdapter(mAdaptor);

Adapters 中使用的视图项是相同的布局,其中一行有两个元素并排使用 layout_width=“0dp”/layout_weight=“1” 拉伸以填充该行。ListViewRecyclerView

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top" >

<LinearLayout
    android:id="@+id/row"
    android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:orientation="horizontal"
android:visibility="gone"
    >
    <include
            android:id="@+id/row_cell_one"
            layout="@layout/cell_layout" 
            android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
        />
    <include
            android:id="@+id/row_cell_two"
            layout="@layout/cell_layout" 
            android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
</LinearLayout>

cell_layout 是这样的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/img_container"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/dumy_img" />

    <ImageView
        android:id="@+id/overlay_img"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        android:background="@drawable/box_border"
        android:scaleType="center"
        android:src="@drawable/overlay_image"
        android:tint="@color/dark_pink"
        android:visibility="gone" />
</RelativeLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/padding_top"
    android:paddingBottom="@dimen/padding_bottom"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/left"
        android:layout_marginRight="@dimen/right"
        android:ellipsize="end"
        android:textSize="@dimen/text_size"
        android:gravity="left"
        android:singleLine="true" 
        android:textColor="@color/black"/>

    <TextView
        android:id="@+id/text_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/left"
        android:layout_marginRight="@dimen/right"
        android:ellipsize="end"
        android:textSize="@dimen/text_size"
        android:gravity="left"
        android:singleLine="true"
        android:textColor="@color/light_blue" />
</LinearLayout>

两者(使用ListViewRecyclerView)都类似于Adapter更新View,除了在RecyclerView实现

public RowViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
public void onBindViewHolder(RowViewHolder holder, int position) 
public int getItemCount()
  1. 首先要注意的是 using 的滚动ListView比使用RecyclerView.

  2. 如果使用LinearLayoutManager,则行中显示的项目混乱。这两个项目不占据显示的整个宽度,并且应该覆盖在其他元素之上的元素(例如img.top == overlay_img.top)被放置在下面(例如overlay_img.top == img.top + img.height),并且在overlay_img.

只有在更改为使用mLayoutManager = new GridLayoutManager(getActivity(), 1);后,该行才能正确显示。

RecyclerView(使用LinearLayoutManager)它显示为:

+---------------------+

(cell | (cell  
 one) |  two)

+---------------------+

使用ListViewand RecyclerView(using GridLayoutManager) 它显示为:

+---------------------+

(cell one) | (cell two)

+---------------------+
4

0 回答 0