gpt4 book ai didi

android - 在回收站 ListView 项目之间添加了大空间

转载 作者:行者123 更新时间:2023-11-30 00:49:51 25 4
gpt4 key购买 nike

每当调用 onActivity 结果时,Recycler listview 在项目之间添加额外的空间。它发生在每个 ListView 中。

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>

最佳答案

实际上,您将 adapter 设置为 recyclerview 的地方是错误的。您必须在 onCreate 方法中使用空 arraylist setAdapter 并且当您在 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;}

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

关于android - 在回收站 ListView 项目之间添加了大空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41235268/

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