2016-12-20 3 views
1

Recycler Listview Hinzufügen zusätzlichen Speicherplatz zwischen den Elementen bei jedem Aufruf onActivity Ergebnis. Es passiert in jeder Listenansicht.Großer Platz zwischen Recyclern Listview-Elementen hinzugefügt

enter image description here

enter image description here

Mein Java-Code

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> 

und Adapter Vorlage 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> 
+0

nur zu schreiben versuchen progressBar enthalten zu entfernen oder setze seine Sichtbarkeit auf GONE. Ich denke, der Space ist nur wegen dieser progressBar. –

+0

nop .. i entfernt, dass progressbar und kontrolliert nach wie vor Probleme gibt es .. –

+0

style = "@ style/kaho_content_small_textview_style" custom: CustomTextViewFont = "@ Zeichenfolge/kaho_segoeui_regular_font" können Sie uns zeigen diese Dateien –

Antwort

1

eigentlich der Ort, wo man adapter-recyclerview setzen, ist falsch. Sie müssen setAdapter innerhalb onCreate Methode mit leeren arraylist und wenn Sie aktualisierte Liste in startActivityResult erhalten Sie nur notifyDataSetChanged. Zu Ihrer Information

innen onCreate

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

dann innerhalb generateList() -Methode

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

ein Verfahren setData innerhalb PendingTaskAdatper erstellen Sie Ihre aktualisierte Liste wie unten

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

Jetzt nehmen Sie sollten Ihre erwartete Ausgabe

erhalten
+0

Dank ein lot..its Arbeiten jetzt –

+0

glücklich, Ihnen zu helfen –

0

entfernen progressbar aus Vorlage template_task_list.xml

0

@Karthik Thunga,

Für den Raum zwischen zwei Artikel von RecycleView minimieren, Sie haben den Code ItemDecoration

uns die folgenden Code-Snippet

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