私はrecylerViewを初めて使っていますが、Uniプロジェクト用に実装しました。 recylcerビューは正常に動作し、各行にimageViewとtexViewのリストがあります。同じ方法を使用して、リストに項目を動的に追加します。Android RecyclerViewは永続的なリスト項目を表示しており、画面に表示されます
2番目の項目をadapter.listに追加すると、ビューは新しいリストを作成し、その上に最初の項目を両方とも表示するように見えます。ここでビューが、私はいくつかの項目を追加した後にどのように見えるかのscreenieです:
私は私が実装 ..私はrecycleViewsに新たなんだ、なぜビューがこれをやっているさっぱりだがすべてのアイテムを一度に追加しても大丈夫だったという単純な見解では、なぜこのようなことが起こるのか分かりません。誰の手がかり?それは私のナッツを運転しています。
私が試したこと: adapter.listをクリアしてリストを再作成する:同じ動作。 RecyclerViewを初期化してリストを再作成する:同じ動作。
ありがとうございます! 編集:row_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardView"
android:layout_below="@+id/tvWelcome"
android:layout_width="320dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
app:cardCornerRadius="@dimen/activity_vertical_margin"
app:cardElevation="@dimen/activity_vertical_margin" >
<!-- id was textView -->
<TextView
android:id="@+id/txtRecycler"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="30dp"
android:text="First Clue will go here "
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/imgShowHTML"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="@android:drawable/ic_menu_info_details"
android:layout_margin="16dp"
android:layout_gravity="end"
android:background="@color/colorAccent"
android:layout_below="@+id/txtRecycler"/>
</android.support.v7.widget.CardView>
とcontent_main.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="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="cave.mike.btle_de.MainActivity"
tools:showIn="@layout/activity_main">
<TextView
android:id="@+id/tvTeamName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_margin="@dimen/layout_margin"
android:text="Team Name"/>
<TextView
android:id="@+id/tvWelcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvTeamName"
android:textSize="@dimen/font_size"
android:layout_margin="@dimen/layout_margin"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:text="@string/welcome_text"/>
<!-- MJC this holds and displays the list of clues as they are found -->
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:layout_below="@+id/tvWelcome"
/>
<!-- Recycler View List-->
<include layout="@layout/row_layout"/>
</RelativeLayout>
そして、これが作成した上recyclerviewアダプタです:
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//Inflate the layout, initialize the View Holder
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);
MyViewHolder holder = new MyViewHolder(v);
return holder;
}
RECYCLERクラス:
public class Recycler_View_Adapter extends RecyclerView.Adapter<Recycler_View_Adapter.MyViewHolder> {
//private List list = Collections.emptyList();
private List list = new ArrayList();
Context context;
private int iCnt =0;
public Recycler_View_Adapter(List list, Context context) {
this.list = list;
this.context = context;
setHasStableIds(false);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//Inflate the layout, initialize the View Holder
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);
MyViewHolder holder = new MyViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
//Use the provided View Holder on the onCreateViewHolder method to populate the current row on the RecyclerView
//List list = new ArrayList();
if(list.get(position).toString() == null) {
holder.recyclerText.setVisibility(View.GONE);
}else {
String s = "";
if (list != null) {
for (int i = 0; i <= list.size() - 1; i++) {
s = list.get(i).toString();
Log.d("onBindViewHolder", "List.item = " + String.valueOf(i) + " Desc: " + s);
}
}
iCnt++;
Log.d("onBindViewHolder", "icnt = " + String.valueOf(iCnt) + "POS = " + String.valueOf(position));
String sDesc = "" + list.get(position);
Log.d("onBindViewHolder", "Position = " + String.valueOf(position) + " DESC:: " + sDesc);
holder.recyclerText.setText(sDesc);
}
}
@Override
public int getItemCount() {
//returns the number of elements the RecyclerView will display
return list.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
// Insert a new item to the RecyclerView on a predefined position
public void insert(int position, String sDesc) {
Log.d("BTLE DE", "RecyclerVIew Insert: position:" + String.valueOf(position) + " Desc: "+ sDesc);
list.add(position, sDesc);
notifyItemInserted(position);
}
// Define listener member variable
private static OnItemClickListener listener;
// Define the listener interface
public interface OnItemClickListener {
void onItemClick(View itemView, int position);
}
// MJC allows the Main (or parent) activity to define the listener - so bubbled up to main
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
public void clearData() {
Log.d("RecyclerView", "INSIDE clearData");
int size = this.list.size();
if (size > 0) {
for (int i = 0; i < size; i++) {
this.list.remove(0);
Log.d("RecyclerView", "INSIDE clearData.List.Remove");
}
this.notifyItemRangeRemoved(0, size);
}
}
public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
public ImageView imgShowHTML;
public TextView recyclerText;
public MyViewHolder(final View itemView) {
super(itemView);
imgShowHTML = (ImageView) itemView.findViewById(R.id.imgShowHTML);
recyclerText = (TextView) itemView.findViewById(R.id.txtRecycler);
itemView.setOnClickListener(this);
imgShowHTML.setOnClickListener(this);
recyclerText.setOnClickListener(this);
}
// onClick Listener for view
@Override
public void onClick(View v) {
/* MJC Listener for image button*/
// Triggers click upwards to the adapter on click
if (listener != null)
//only pass it up if button has been clicked
if (v.getId() == imgShowHTML.getId()) {
Toast.makeText(v.getContext(), "ITEM/ROW PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
listener.onItemClick(itemView, getLayoutPosition());
}
}
@Override
public boolean onLongClick(View v) { //must have long click support
return false;
}
}
}
xmlを投稿することはできますか?あなたがファンキーなことをしない限り、RecyclerViewの一部ではないようです。 – zgc7009
done、looking for looking –
あなたの問題は、content_mainの最後にあるこの ' 'です。これは、あなたの親レイアウトにrow_layoutビューを追加することです。あなたのアダプタは、RecyclerViewにビューを動的に追加します。そのような親レイアウトにホルダーとして追加する必要はありません。 –
zgc7009