在回收者列表视图项之间添加了大量空间

时间:2016-12-20 05:09:09

标签: android android-fragments android-recyclerview

回收站列表视图在调用onActivity Result时在项目之间添加额外空间。它发生在每个列表视图中。

enter image description here

enter image description here

我的java代码

package com.kahoindia.dev.fragments;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.kahoindia.dev.activities.KaHOAppController;
import com.kahoindia.dev.activities.KaHOComposeTaskActivity;
import com.kahoindia.dev.activities.KaHOTaskDetailActivity;
import com.kahoindia.dev.activities.R;
import com.kahoindia.dev.customclasses.KaHOTextView;
import com.kahoindia.dev.customclasses.RecyclerTouchListener;
import com.kahoindia.dev.helpers.Const;
import com.kahoindia.dev.helpers.DateUtility;
import com.kahoindia.dev.helpers.LayoutUtility;
import com.kahoindia.dev.interfaces.KaHOIRecyclerViewItemClickListener;
import com.kahoindia.dev.interfaces.KaHOViewInterface;
import com.kahoindia.dev.managerclasses.KaHObTicket;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by KaHO PC-1 on 12-08-2016.
 */
public class KaHOPendingTaskFragment extends KaHOFragment {

    private RecyclerView mRecyclerView;
    private JSONArray mPendingTaskArray;
    private boolean mOnLoad = false;
    private PendingTaskAdapter mAdapter;
    private FloatingActionButton mBtnAssignTask;
    private boolean mRecExists;
    private int mPageNum;
    private int mHolderPos;
    private PendingTaskAdapter.ViewHolder mHolder;
    private ProgressBar mProgressbar;
    private String mTotalPages;
    private KaHOTextView mTxtNoData;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_task_pending_list, container, false);
        try {
            mRecyclerView = (RecyclerView) view.findViewById(R.id.pendingList);
            mBtnAssignTask = (FloatingActionButton) view.findViewById(R.id.assignTask);
            mProgressbar = (ProgressBar) view.findViewById(R.id.progress_bar);
            mTxtNoData = (KaHOTextView) view.findViewById(R.id.txtNoData);
            mRecExists = true;
            mOnLoad = true;
            mPageNum = 1;
            mHolderPos = 0;
            mAdapter = new PendingTaskAdapter();
            mPendingTaskArray = new JSONArray();
            mRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), mRecyclerView, new KaHOIRecyclerViewItemClickListener() {
                @Override
                public void onClick(View view, int position) {
                    try {
                        JSONObject selectedObject = mPendingTaskArray.getJSONObject(position);
                        Intent intent = new Intent(getActivity(), KaHOTaskDetailActivity.class);
                        intent.putExtra("taskObject", selectedObject.toString());
                        startActivityForResult(intent, 1);
                    } catch (Exception e) {
                        e.getStackTrace();
                    }
                }

                @Override
                public void onLongClick(View view, int position) {

                }
            }));

            mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                int currentFirstVisibleItem;
                int currentVisibleItemCount;
                int currentScrollState;

                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    this.currentScrollState = newState;
                    isScrollCompleted();
                }

                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    this.currentFirstVisibleItem = dx;
                    this.currentVisibleItemCount = dy;
                }

                private void isScrollCompleted() {
                    try {
                        if (this.currentVisibleItemCount > 0 && mRecExists && this.currentScrollState == RecyclerView.SCROLL_STATE_IDLE) {
                            mPageNum++;
                            mOnLoad = false;
                            if (mPageNum <= Integer.parseInt(mTotalPages)) {
                                mHolder = (PendingTaskAdapter.ViewHolder) mRecyclerView.findViewHolderForAdapterPosition(mHolderPos);
                                mHolder.pBar.setVisibility(View.VISIBLE);
                                getPendingTask();
                            }


                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

            mBtnAssignTask.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), KaHOComposeTaskActivity.class);
                    startActivityForResult(intent, 2);
                }
            });

            getPendingTask();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return view;
    }

    public void getDataOnLoad() {
        if (mOnLoad) {
            getPendingTask();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.setActivityTag(getActivity().getLocalClassName());

        if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
            mHolder = null;
            mOnLoad = true;
            mPendingTaskArray = new JSONArray();
            mPageNum = 1;
            getPendingTask();
        } else if (requestCode == 2 && resultCode == Activity.RESULT_OK) {
            mHolder = null;
            mOnLoad = true;
            mPendingTaskArray = new JSONArray();
            mPageNum = 1;
            getPendingTask();
        }

    }

    private void getPendingTask() {
        HashMap<String, String> params = new HashMap<>();
        params.put("ayCode", KaHOAppController.getCurrentAY());
        params.put("Mine", "1");
        params.put("Others", "0");
        params.put("Is_Pending", "1");
        params.put("PageNo", String.valueOf(mPageNum));
        params.put("NoOfRec", String.valueOf(Const.mNumOfRecords));
        if (mOnLoad) {
            mProgressbar.setVisibility(View.VISIBLE);
        }
        KaHObTicket kaHObTicket = new KaHObTicket();
        kaHObTicket.getTask(params, "GetTaskList", mContext, new KaHOViewInterface() {
            @Override
            public void viewHandler(Object viewResponse) {
                JSONObject responseObject = (JSONObject) viewResponse;
                try {

                    if (!mOnLoad && mHolder != null) {
                        mHolder.pBar.setVisibility(View.GONE);
                    } else {
                        mProgressbar.setVisibility(View.GONE);
                    }

                    String msg = responseObject.getString("msg");
                    String cusmsg = responseObject.getString("cusmsg");
                    String code = responseObject.getString("code");
                    if (msg.equals("Success") && code.equals(Const.SUCCESS_CODE)) {
                        JSONArray array = responseObject.getJSONArray("GetTaskList");
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject jsonObject = array.getJSONObject(i);
                            if (i == 0) {

                                mTotalPages = jsonObject.getString("Total_Pages");
                            }
                            mPendingTaskArray.put(jsonObject);
                        }
                        mRecExists = true;

                        generateList();
                    } else {
                        mRecExists = false;
                        if (mOnLoad) {
                            mTxtNoData.setVisibility(View.VISIBLE);
                            mTxtNoData.setText(cusmsg);
                        }

                    }
                    mOnLoad = false;
                } catch (Exception e) {
                    mProgressbar.setVisibility(View.GONE);
                    e.printStackTrace();
                }
            }
        });
    }

    private void generateList() {
        try {
            if (mOnLoad) {
                LayoutUtility.setRecyclerListView(mRecyclerView, mContext);
                mRecyclerView.setAdapter(mAdapter);
            } else {
                mAdapter.notifyDataSetChanged();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public class PendingTaskAdapter extends RecyclerView.Adapter<PendingTaskAdapter.ViewHolder> {
        public class ViewHolder extends RecyclerView.ViewHolder {
            public KaHOTextView txtAssignedBy, txtAssignedTo, txtTitle, txtDate;
            public ProgressBar pBar;

            public ViewHolder(View view) {
                super(view);
                txtAssignedBy = (KaHOTextView) view.findViewById(R.id.txtAssignedBy);
                txtAssignedTo = (KaHOTextView) view.findViewById(R.id.txtAssignedTo);
                txtTitle = (KaHOTextView) view.findViewById(R.id.txtTitle);
                txtDate = (KaHOTextView) view.findViewById(R.id.txtDate);
                pBar = (ProgressBar) view.findViewById(R.id.progressBar);
            }
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.template_task_list, parent, false);
            return new ViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, final int position) {
            try {
                mHolderPos = position;
                JSONObject taskObject = mPendingTaskArray.getJSONObject(position);
                holder.txtAssignedBy.setText(taskObject.getString("Created_By_Name"));
                holder.txtAssignedTo.setText(taskObject.getString("Assigned_To_Name"));
                holder.txtTitle.setText(taskObject.getString("Title"));
                holder.txtDate.setText(DateUtility.changeDateFormat(taskObject.getString("Date"), "dd/MM/yyyy", "dd MMM"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public int getItemCount() {
            return mPendingTaskArray.length();
        }
    }


}

fragment_task_pending_list.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:custom="http://schemas.android.com/tools">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/pendingList"/>

    <include
        android:id="@+id/progress_bar"
        layout="@layout/progress_bar"></include>

    <com.kahoindia.dev.customclasses.KaHOTextView
        android:id="@+id/txtNoData"
        style="@style/kaho_content_label_textview_style"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center_horizontal"
        android:text="@string/task_no_data"
        android:visibility="gone"
        custom:CustomTextViewFont="@string/kaho_segoeui_regular_font" />


    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp"
        app:backgroundTint="@color/floatingIconRed"
        android:id="@+id/assignTask"
        android:src="@drawable/floating_plus"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

和适配器模板template_task_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="wrap_content"
    xmlns:custom="http://schemas.android.com/apk/res-auto">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingLeft="20dp"
        android:paddingRight="20dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <com.kahoindia.dev.customclasses.KaHOTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/kaho_listview_label_heading"
                android:text="From : "
                custom:CustomTextViewFont="@string/kaho_segoeui_semi_light_font"/>

            <com.kahoindia.dev.customclasses.KaHOTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/kaho_listview_label_heading"
                android:id="@+id/txtAssignedBy"
                custom:CustomTextViewFont="@string/kaho_segoeui_semi_light_font"/>

            <com.kahoindia.dev.customclasses.KaHOTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/kaho_listview_label_heading"
                android:text="To : "
                custom:CustomTextViewFont="@string/kaho_segoeui_semi_light_font"
                android:paddingLeft="10dp"/>

            <com.kahoindia.dev.customclasses.KaHOTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/kaho_listview_label_heading"
                android:id="@+id/txtAssignedTo"
                custom:CustomTextViewFont="@string/kaho_segoeui_semi_light_font"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="5dp"
            android:weightSum="2">


            <com.kahoindia.dev.customclasses.KaHOTextView
                android:id="@+id/txtTitle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ellipsize="marquee"
                android:singleLine="true"
                style="@style/kaho_content_small_textview_style"
                custom:CustomTextViewFont="@string/kaho_segoeui_regular_font"/>

            <com.kahoindia.dev.customclasses.KaHOTextView
                android:id="@+id/txtDate"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                style="@style/kaho_content_small_textview_style"
                custom:CustomTextViewFont="@string/kaho_segoeui_regular_font"
                android:gravity="right" />

        </LinearLayout>
        <include
            android:id="@+id/progressBar"
            layout="@layout/lazy_loading_progress_bar"/>

    </LinearLayout>

</LinearLayout>

3 个答案:

答案 0 :(得分:1)

实际上,您将adapter设置为recyclerview的位置是错误的。您必须在setAdapter方法内onCreate使用空arraylist,当您在startActivityResult中获得更新列表时,您只需要notifyDataSetChanged。供您参考

在onCreate里面

  mAdapter = new PendingTaskAdapter(new ArrayList<YourListClassName>(); // empty arraylist
  mRecyclerView.setAdatper(mAdapter);

然后在generateList()方法里面

  mAdapter.setData(YourUpdatedList);
  mAdapter.notifyDataSetChanged();

在PendingTaskAdatper中创建一个方法setData,以获取如下所示的更新列表

   private void setData(List<YourListClassName> updatedList){
   mUpdatedList = updatedList;}

现在你应该得到预期的输出

答案 1 :(得分:0)

从模板template_task_list.xml中删除进度条

答案 2 :(得分:0)

@Karthik Thunga,

为了最小化两个RecycleView项之间的空间,您必须编写ItemDecoration的代码

使用以下代码段

mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(homeActivity)
                .color(Color.TRANSPARENT)
                .sizeResId(R.dimen.padding_5)
                .build());