滑动布局上的Android Recyclerview会留下空白

时间:2016-08-06 07:08:25

标签: android android-recyclerview

我正在使用this从右侧使用滑动布局实施recyclerview。我已经在第一个位置隐藏了adapter中的一个子视图,但是问题是,一旦它成功隐藏,从右侧滑动时显示空白空白。 下面是我的适配器view.xml。下面我附上图片,您可以在其中看到确切的问题。

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <!--for background layout-->

    <LinearLayout
    android:id="@+id/rowBG"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:gravity="right"
    android:orientation="horizontal">


    <RelativeLayout
        android:id="@+id/add"
        android:layout_width="@dimen/swipeWidth"
        android:layout_height="match_parent"
        android:background="@color/swipeoption_blue"
        android:clickable="true"
        android:focusable="true"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:background="?android:selectableItemBackground"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@drawable/ic_add_black_24dp"
                android:tint="@android:color/white"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="4dp"
                android:ellipsize="end"
                android:gravity="center"
                android:maxLines="1"
                android:text="Add"
                android:textColor="@android:color/white"
                android:textSize="12sp"/>
        </LinearLayout>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/edit"
        android:layout_width="@dimen/swipeWidth"
        android:layout_height="match_parent"
        android:background="@color/swipeoption_green"
        android:clickable="true"
        android:focusable="true"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:background="?android:selectableItemBackground"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/ic_mode_edit_black_24dp"
                android:tint="@android:color/white"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="4dp"
                android:ellipsize="end"
                android:gravity="center"
                android:maxLines="1"
                android:text="Edit"
                android:textColor="@android:color/white"
                android:textSize="12sp"/>
        </LinearLayout>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/change"
        android:layout_width="@dimen/swipeWidth"
        android:layout_height="match_parent"
        android:background="@color/swipeoption_purple"
        android:clickable="true"
        android:focusable="true"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:background="?android:selectableItemBackground"
            android:gravity="center_vertical|left"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:tint="@android:color/white"
                app:srcCompat="@drawable/ic_build_black_24dp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="4dp"
                android:ellipsize="end"
                android:gravity="center"
                android:maxLines="1"
                android:text="Change"
                android:textColor="@android:color/white"
                android:textSize="12sp"/>
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>



<!--for Forground layout-->

    <LinearLayout
        android:id="@+id/rowFG"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:clickable="true"
        android:elevation="4dp"
        android:focusable="true"
        android:orientation="horizontal"
        android:visibility="visible">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/selectableItemBackground"
            android:orientation="horizontal"
            android:paddingBottom="16dp"
            android:paddingLeft="16dp"
            android:paddingTop="16dp">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="6"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/mainText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:maxLines="1"
                    android:text="No Calls Found"
                    android:textSize="16sp"
                    android:textStyle="bold"
                    tools:text="Row 1"/>

                <TextView
                    android:id="@+id/subText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="2dp"
                    android:maxLines="1"
                    tools:text="Some text ..."/>
            </LinearLayout>

            <Button
                android:id="@+id/rowButton"
                style="?attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right|center_vertical"
                android:gravity="right|center_vertical"
                android:text="Button"/>
        </LinearLayout>

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true"
        android:background="#67676767"
        android:elevation="5dp"/>
</RelativeLayout>

这是我的活动

public class MainActivity extends Activity implements RecyclerTouchListener.RecyclerTouchListenerHelper {

private RecyclerView mRecyclerView;
private ArrayList<MyModel> arrayList = new ArrayList<MyModel>();
private Adapter mAdapter;
protected Handler handler;
private MyModel model;
private int test = 1;

private Boolean flag = true;
private RecyclerTouchListener onTouchListener;
private OnActivityTouchListener touchListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    setRecycler();
    setTouchListner();

}


private void setTouchListner() {

    onTouchListener = new RecyclerTouchListener(MainActivity.this, mRecyclerView);
    onTouchListener
            .setIndependentViews(R.id.rowButton)
            .setViewsToFade(R.id.rowButton)
            .setClickable(new RecyclerTouchListener.OnRowClickListener() {
                @Override
                public void onRowClicked(int position) {
                    Log.e("Row clicked ","!!!!!!!!");
                }

                @Override
                public void onIndependentViewClicked(int independentViewID, int position) {
                    Toast.makeToast(getApplicationContext(), "Button in row " + (position + 1) + " clicked!");
                }
            })
            .setLongClickable(true, new RecyclerTouchListener.OnRowLongClickListener() {
                @Override
                public void onRowLongClicked(int position) {
                    Log.e("Row Long clicked ","@@@@@@@@@@");

                }
            })
            .setSwipeOptionViews(R.id.add, R.id.edit, R.id.change)
            .setSwipeable(R.id.rowFG, R.id.rowBG, new RecyclerTouchListener.OnSwipeOptionsClickListener() {
                @Override
                public void onSwipeOptionClicked(int viewID, int position) {
                        String message = "";
                    if (viewID == R.id.add) {

                        Log.e("Add ", "Clicked ");

                    } else if (viewID == R.id.edit) {

                        Log.e("Edit ", "Clicked ");

                    }


                    Log.v("log", "POSSSSSSSSS: " + position);
                }
            });

}

private void setRecycler() {

    createlist();  // in this method, Create a list of items.

    final LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    mRecyclerView.setLayoutManager(layoutManager);

    mRecyclerView.setHasFixedSize(true);

    mAdapter = new Adapter(arrayList, mRecyclerView,MainActivity.this);
    mRecyclerView.setAdapter(mAdapter);
    handler = new Handler();

    mAdapter.setOnLoadMoreListener(new Adapter.OnLoadMoreListener() {
        @Override
        public void onLoadMore() {
            //add progress item
            arrayList.add(null);
            mAdapter.notifyItemInserted(arrayList.size() - 1);

            handler.postDelayed(new Runnable() {
                @Override
                public void run() {

                    flag = false;

                    //remove progress item
                    arrayList.remove(arrayList.size() - 1);
                    mAdapter.notifyItemRemoved(arrayList.size());

                    createlist();

                    mAdapter.notifyItemInserted(arrayList.size());
                    mAdapter.setLoaded();

                }
            }, 2000);
            System.out.println("load");
            flag = true;
        }
    });

}

// this method is used to create list of items.
public void createlist() {
    for (int i = 1; i <= 10; i++) {
        model = new MyModel();
        model.setStrFname("Testing" + test);
        model.setStrLname("Testing");
        arrayList.add(model);
        test++;
    }
}

// Below all methond is used for swipe from right using library.
@Override
public void setOnActivityTouchListener(OnActivityTouchListener listener) {
    this.touchListener = listener;
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (flag) {
        return false;
    } else {
        if (touchListener != null) {
            touchListener.getTouchCoordinates(ev);
        }
        return super.dispatchTouchEvent(ev);
    }
}

@Override
protected void onResume() {
    super.onResume();
    mRecyclerView.addOnItemTouchListener(onTouchListener);
}

@Override
protected void onPause() {
    super.onPause();
    mRecyclerView.removeOnItemTouchListener(onTouchListener);
}

}

和我的适配器

    public class Adapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final int VIEW_ITEM = 1;
    private final int VIEW_PROG = 0;

    private List<MyModel> mDataset;
    private MyModel model;

    // The minimum amount of items to have below your current scroll position before loading more.
    private int visibleThreshold = 2;
    private int lastVisibleItem, totalItemCount;
    private boolean loading;
    private OnLoadMoreListener onLoadMoreListener;
    private MainActivity mainActivity;


    public Adapter(List<MyModel> myDataSet, RecyclerView recyclerView, MainActivity mainActivity) {
        mDataset = myDataSet;
        this.mainActivity = mainActivity;

        if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {

            final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
            recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);

                    totalItemCount = linearLayoutManager.getItemCount();
                    lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
                    if (!loading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
                        // End has been reached
                        // Do something
                        if (onLoadMoreListener != null) {
                            onLoadMoreListener.onLoadMore();
                        }
                        loading = true;
                    }
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return mDataset.get(position) != null ? VIEW_ITEM : VIEW_PROG;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder vh;
        if (viewType == VIEW_ITEM) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_row, parent, false);

            vh = new TextViewHolder(itemView);

        } else {
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.progressbar_item, parent, false);
            vh = new ProgressViewHolder(v);
        }
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof TextViewHolder) {
            Log.v("log", "POS: " + position);

            // Here I have hide the first postion of recyclerView
            if(position==1)
            {
                ((TextViewHolder) holder).change.setVisibility(View.GONE);
            }

            model = mDataset.get(position);
            holder.setIsRecyclable(false);

            ((TextViewHolder) holder).mainText.setText(model.getStrFname());
            ((TextViewHolder) holder).subText.setText(model.getStrLname());

        } else {
            ((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
        }
    }

    public void setLoaded() {
        loading = false;
    }

    @Override
    public int getItemCount() {
        return mDataset.size();
    }

    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        this.onLoadMoreListener = onLoadMoreListener;
    }

    public interface OnLoadMoreListener {
        void onLoadMore();
    }

    public static class TextViewHolder extends RecyclerView.ViewHolder {

        TextView mainText, subText;
        RelativeLayout change;
        LinearLayout rowBG;

        public TextViewHolder(View itemView) {
            super(itemView);
            mainText = (TextView) itemView.findViewById(R.id.mainText);
            subText = (TextView) itemView.findViewById(R.id.subText);
            change = (RelativeLayout) itemView.findViewById(R.id.change);

            rowBG = (LinearLayout) itemView.findViewById(R.id.rowBG);
        }
    }

    public static class ProgressViewHolder extends RecyclerView.ViewHolder {
        public ProgressBar progressBar;

        public ProgressViewHolder(View v) {
            super(v);
            progressBar = (ProgressBar) v.findViewById(R.id.progressBar);
        }
    }
}

enter image description here

0 个答案:

没有答案