我无法弄清楚为什么我的RecyclerView的第一行只显示空的xml-Layout。
就像该行的数据是空的一样。但我得到4行 - 即使我只有3行数据记录。 。 (行大小用“data-record”-List.size();)
指定无论如何,我正在寻找一种解决方法。有谁知道如何只删除Recycler View的第一行?我是否只需要删除“data-record
”列表的第一项?
问候约翰
编辑代码:
VotingHistoryFragment.java:
public class VotingHistoryFragment extends Fragment implements VotingHistoryAdapter.ClickListener{
private Realm realm;
private FragmentStateHandler fragmentStateHandler;
private Backend backend;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
super.onCreateView(inflater, container, savedInstanceState);
View layout = inflater.inflate(R.layout.voting_history_fragment_layout, container, false);
HotFixRecyclerView hotFixRecyclerView = (HotFixRecyclerView) layout.findViewById(R.id.votingHistoryDrawerList);
realm = Realm.getInstance(getActivity());
fragmentStateHandler = FragmentStateHandler.getInstance();
backend = Backend.getInstance();
VotingHistoryAdapter votingHistoryAdapter = new VotingHistoryAdapter(getActivity());
votingHistoryAdapter.setClickListener(this);
hotFixRecyclerView.setAdapter(votingHistoryAdapter);
hotFixRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return layout;
}
private RealmList<Voting> getVotingList() {
RealmList<Voting> votingList = new RealmList<>();
votingList.addAll(realm.where(Voting.class).equalTo(Constants.TAG_STATUS, false).findAllSorted(Constants.TAG_TIMESTAMP));
return votingList;
}
@Override
public void itemClicked(View view, int position) {
try {
Voting current = getVotingList().get(position);
Bundle b = new Bundle();
b.putString(Constants.TAG_OWNER_ID, backend.loggedOwner.getId());
b.putString(Constants.TAG_VOTING_ID, current.getId());
fragmentStateHandler.replaceFrag(Frag.ARCHIVEDVOTINGFRAGMENT, b);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onPause() {
super.onPause();
SharedPreferences prefs = getActivity().getSharedPreferences("X", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("lastActivity", getClass().getName());
editor.apply();
}
}
VotingHistoryAdapter.java:
public class VotingHistoryAdapter extends HotFixRecyclerView.Adapter <VotingHistoryAdapter.votingHistoryViewHolder>{
private final LayoutInflater inflater;
private final RealmList<Voting> votingList;
private ClickListener clickListener;
private final Realm realm;
private int increment;
public VotingHistoryAdapter(Context context) {
inflater = LayoutInflater.from(context);
realm = Realm.getInstance(context);
realm.setAutoRefresh(false);
// query votings
RealmQuery<Voting> votingRealmQuery = realm.where(Voting.class);
votingList = new RealmList<>();
votingList.addAll(votingRealmQuery.equalTo("status", false).findAll());
increment = 0;
}
@Override
public votingHistoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.voting_history_row, parent, false);
return new votingHistoryViewHolder(view);
}
@Override
public void onBindViewHolder(votingHistoryViewHolder holder, int position) {
Voting current = votingList.get(position);
RealmList<Idea> ideaRealmList = new RealmList<>();
ideaRealmList.addAll(realm.where(Idea.class).equalTo(Constants.TAG_VOTING_ID, current.getId()).findAllSorted(Constants.TAG_VOTECOUNT, false));
Idea winnerIdea = ideaRealmList.first();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(current.getTimeStamp());
int mYear = calendar.get(Calendar.YEAR);
int mMonth = calendar.get(Calendar.MONTH);
int mDay = calendar.get(Calendar.DAY_OF_MONTH);
if(winnerIdea==null){Log.d("winnerIdea ", "Idea is null"); return;}
try {
Log.d("winnerIdea ", "position:" + position);
Log.d("winnerIdea ", "titel:" + winnerIdea.getText());
Log.d("winnerIdea ", "text:" + winnerIdea.getText());
Log.d("winnerIdea ", "tag1:" + winnerIdea.getTags().get(0).getText());
Log.d("winnerIdea ", "tag2:" + winnerIdea.getTags().get(1).getText());
Log.d("winnerIdea ", "tag3:" + winnerIdea.getTags().get(2).getText());
Log.d("winnerIdea ", "tag4:" + winnerIdea.getTags().get(3).getText());
Log.d("winnerIdea ", "besitzer: " + winnerIdea.getOwner().getName());
Log.d("winnerIdea ", "voteCount: " + winnerIdea.getVoteCount());
} catch (Exception e) {
e.printStackTrace();
Log.d("winnerIdea ", "winnerIdea is null!!");
}
try {
holder.voteCardNr.setText(" " + (++increment));
holder.voteCardWinnerIdeaOwner.setText(" " + winnerIdea.getOwner().getName());
try {
holder.voteCardWinnerIdeaVotes.setText(" " + winnerIdea.getVoteCount());
} catch (Exception e) {
e.printStackTrace();
holder.voteCardWinnerIdeaVotes.setText(" " + "0");
}
holder.voteCardWinnerIdea.setText(" " + winnerIdea.getTitle());
holder.voteCardDatum.setText(" am " + mDay + "." + mMonth + "." + mYear);
} catch (Exception e) {
e.printStackTrace();
}
}
public void setClickListener (ClickListener clickListener) {
this.clickListener = clickListener;
}
@Override
public int getItemCount() {
return votingList.size();
}
class votingHistoryViewHolder extends HotFixRecyclerView.ViewHolder implements View.OnClickListener{
TextView voteCardNr;
TextView voteCardWinnerIdeaOwner;
TextView voteCardWinnerIdea;
TextView voteCardWinnerIdeaVotes;
TextView voteCardDatum;
public votingHistoryViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
VotingHistoryCardLayout layout = (VotingHistoryCardLayout) itemView.findViewById(R.id.voteRecycleCardLayout);
List<TextView> views = layout.getVotingHistoryCards();
try {
voteCardNr = views.get(0);
voteCardWinnerIdea = views.get(1);
voteCardWinnerIdeaOwner = views.get(2);
voteCardWinnerIdeaVotes = views.get(3);
voteCardDatum = views.get(4);
}catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
if(clickListener != null) {
clickListener.itemClicked(v, getAdapterPosition());
}
}
}
public interface ClickListener{
void itemClicked(View view, int position);
}
}
voting_history_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/asdasd">
<smoca.ch.kreagen.layouts.VotingHistoryCardLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/voteRecycleCardLayout"
android:orientation="vertical"
/>
</LinearLayout>
VotingHistoryCardLayout.java:
public class VotingHistoryCardLayout extends LinearLayout{
private ArrayList<TextView> views;
// three different constructors to fit any kind of invoke
public VotingHistoryCardLayout(Context context) {
super(context);
init();
}
public VotingHistoryCardLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public VotingHistoryCardLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
// Initialize the Layout
private void init() {
views = new ArrayList<>();
LayoutInflater li = LayoutInflater.from(getContext());
// Dimension: 1x3
LinearLayout newLayout = new LinearLayout(getContext());
// set Orientation for each row
newLayout.setGravity(Gravity.CENTER_HORIZONTAL);
newLayout.setOrientation(LinearLayout.HORIZONTAL);
this.addView(newLayout);
// create new Card
CardView card1 = (CardView) li.inflate(R.layout.voting_history_cards, newLayout, false);
// set card color
card1.setCardBackgroundColor(getResources().getColor(R.color.Green));
// add Views to newLayout
newLayout.addView(card1);
views.add((TextView) findViewById(R.id.voteCardNrContent)); // index 0
views.add((TextView)card1.findViewById(R.id.voteCardWinnerIdeaContent)); // index 1
views.add((TextView)card1.findViewById(R.id.voteCardOwnerContent)); // index 2
views.add((TextView)card1.findViewById(R.id.voteCardVotesContent)); // index 3
views.add((TextView)card1.findViewById(R.id.voteCardDateContent)); // index 4
}
public ArrayList<TextView> getVotingHistoryCards() {
return views;
}
}
voting_history_cards.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="8dp"
android:layout_margin="10dp"> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_nr"
android:id="@+id/voteCardNrLabel"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="@+id/voteCardNrContent"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/voteCardNrLabel" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_winnerIdea"
android:id="@+id/voteCardWinnerIdeaLabel"
android:layout_below="@+id/voteCardNrLabel"
android:layout_alignStart="@+id/voteCardNrLabel" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="KreaGen"
android:id="@+id/voteCardWinnerIdeaContent"
android:layout_above="@+id/voteCardOwnerLabel"
android:layout_toEndOf="@+id/voteCardWinnerIdeaLabel" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_winner"
android:id="@+id/voteCardOwnerLabel"
android:layout_below="@+id/voteCardWinnerIdeaLabel"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Joris"
android:id="@+id/voteCardOwnerContent"
android:layout_alignBaseline="@+id/voteCardOwnerLabel"
android:layout_alignBottom="@+id/voteCardOwnerLabel"
android:layout_toEndOf="@+id/voteCardOwnerLabel" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_Votes"
android:id="@+id/voteCardVotesLabel"
android:layout_below="@+id/voteCardOwnerLabel"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/voteCardVotesContent"
android:layout_alignBaseline="@+id/voteCardVotesLabel"
android:layout_alignBottom="@+id/voteCardVotesLabel"
android:layout_toEndOf="@+id/voteCardVotesLabel" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_Date"
android:id="@+id/voteCardDateLabel"
android:layout_below="@+id/voteCardVotesLabel"
android:layout_alignStart="@+id/voteCardVotesLabel" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20.02.1997"
android:id="@+id/voteCardDateContent"
android:layout_below="@+id/voteCardVotesContent"
android:layout_toEndOf="@+id/voteCardDateLabel" />
</RelativeLayout>
</android.support.v7.widget.CardView>
答案 0 :(得分:1)
的确,你的第一个空排有点奇怪。我可以看到的唯一原因是,或者您的请求要么配置了RealmList类,以便返回空的第一行。仔细检查一下。
否则,要删除RecyclerView中的第一行,它非常简单:删除在适配器中分配数组之前的第一项。
更改你的getVotingList函数:
private RealmList<Voting> getVotingList() {
RealmList<Voting> votingList = new RealmList<>();
if(votingList.size() > 0){
votingList.remove(0);
}
votingList.addAll(realm.where(Voting.class).equalTo(Constants.TAG_STATUS, false).findAllSorted(Constants.TAG_TIMESTAMP));
return votingList;
}