RecyclerView childView始终具有相同的ID

时间:2015-10-30 17:08:54

标签: java android xml android-layout android-recyclerview

我的问题 我在RecyclerView中的所有视图,我所说的元素(TextView和ImageView ....)显示得很好,正常,但是当我想列出recyclerView中的所有项目时:

for(int i=0;i<recyclerView.getChildCount();i++){
    ids=ids+"-"+recyclerView.getChildAt(i).getId();
}

当我显示id总是向我显示相同的id,它是RelativeLayout id并且它用来显示RecyclerView中的元素的id作为TextView,ImageView和更多

我的研究

我正在关注来自

幻灯片的视频
  

RecyclerView.OnItemTouchListener

并且对他来说工作正常我不知道为什么我的工作不正常我正在阅读很多关于我的问题以找到确切的问题因为:

  1. 我做的第一件事就是将所有布局的ID名称更改为
  2. 移动我的代码的位置
  3. 确认MotionEvent e.getX()和y.getY()发送的数据是否正确,甚至认为我更改了getRaw的方法
  4. 阅读RecyclerView类,发现此方法中没有问题findChildVIewUnder因为所有事情都是数学的
  5. 读取R文件中每个重要资源的ID号,然后将其转换为dec以比较提示
  6. 我接近沮丧 我的代码
  7.   

    teacher_item

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/problem"
    android:layout_width="328dp"
    android:layout_height="130dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="10dp"
    android:clickable="true"
    android:background="@drawable/borders">
    <ImageView
        android:layout_width="90dp"
        android:layout_height="100dp"
        android:id="@+id/teacher_item_picture"
        style="@style/TeacherItemImage" />
    
    <RatingBar
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:id="@+id/teacher_item_ratingbar"
        style="@style/TeacherItemRatingBar" />
    
    <TextView
        android:id="@+id/teacher_item_name"
        android:layout_width="180dp"
        android:layout_height="16.5dp"
        style="@style/TeacherItemName" />
    
    <TextView
        android:id="@+id/teacher_item_courses"
        android:layout_width="180dp"
        android:layout_height="33dp"
        style="@style/TeacherItemCourses" />
    
    <TextView
        android:id="@+id/teacher_item_faculties"
        android:layout_width="180dp"
        android:layout_height="33dp"
        style="@style/TeacherItemFaculties" />
    
    </RelativeLayout>
    
      

    recyclerview.xml

    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
        android:layout_width="wrap_content"
        android:id="@+id/generic_listView"
        android:layout_height="wrap_content"
        style="@style/GenericListItem">
    </android.support.v7.widget.RecyclerView>
    </RelativeLayout>
    
      

    teacherListFragment.java 重要代码

    teacherListAdapter=new TeacherListAdapter(getActivity(),getData());
        teacherListAdapter.setListener(this);
        recyclerView.addItemDecoration(new RecyclerViewDivider(getActivity()));
        recyclerView.setAdapter(teacherListAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addOnItemTouchListener(new RecyclerViewClickListener(getActivity(), recyclerView, new RecyclerViewClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Log.d("D", view.getId() + "-" + position);
            }
        }));
    

    getData()返回一个List,此代码位于onCreateView 中 的 TeacherListAdatper

    package wan.wanmarcos.views.adapters;
    
    import android.content.Context;
    
    import android.support.v7.widget.RecyclerView;
    import android.text.Html;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.RatingBar;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.squareup.picasso.Picasso;
    
    import java.util.Collections;
    import java.util.List;
    import java.util.Locale;
    
    import wan.wanmarcos.R;
    import wan.wanmarcos.managers.ItemAdapterListener;
    import wan.wanmarcos.managers.ViewHolderSetters;
    import wan.wanmarcos.models.Teacher;
    import wan.wanmarcos.utils.Constants;
    import wan.wanmarcos.views.widgets.CircleTransform;
    public class TeacherListAdapter extends RecyclerView.Adapter<TeacherListAdapter.TeacherHolder> {
        private LayoutInflater inflater;
        private List<Teacher> teachers= Collections.emptyList();
        private ItemAdapterListener itemAdapterListener;
        public TeacherListAdapter(Context context,List<Teacher> teachers){
            inflater= LayoutInflater.from(context);
            this.teachers=teachers;
        }
        @Override
        public TeacherHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view=inflater.inflate(Constants.TEACHER_NEW_ITEM, parent, false);
            TeacherHolder teacherCustomer = new TeacherHolder(view);
            return teacherCustomer;
        }
    
        @Override
        public void onBindViewHolder(TeacherHolder holder, int position) {
            Teacher teacher=teachers.get(position);
            holder.setElements(teacher);
        }
    
        public void setListener(ItemAdapterListener listener){
            this.itemAdapterListener =listener;
        }
    
        @Override
        public int getItemCount() {
            return teachers.size();
        }
    
        public class  TeacherHolder extends RecyclerView.ViewHolder implements ViewHolderSetters<Teacher>,View.OnClickListener{
            private TextView teacherName;
            private RatingBar teacherRating;
            private ImageView teacherImage;
            private TextView teacherCourses;
            private TextView teacherAssumptions;
            private Teacher current;
            public TeacherHolder(View itemView) {
                super(itemView);
                itemView.setOnClickListener(this);
                teacherName= (TextView) itemView.findViewById(R.id.teacher_item_name);
                teacherRating=(RatingBar)itemView.findViewById(R.id.teacher_item_ratingbar);
                teacherImage=(ImageView)itemView.findViewById(R.id.teacher_item_picture);
                teacherCourses=(TextView)itemView.findViewById(R.id.teacher_item_courses);
                teacherAssumptions=(TextView)itemView.findViewById(R.id.teacher_item_faculties);
            }
    
            @Override
            public void setElements(Teacher elements) {
                current=elements;
                teacherName.setText(elements.getName());
                teacherRating.setRating(elements.getRaiting());
                teacherCourses.setText(itemView.getResources().getString(R.string.teacher_capabilities)+ elements.getFaculties());
                teacherAssumptions.setText(itemView.getResources().getString(R.string.teacher_ped) + elements.getDescription());
                Picasso.with(itemView.getContext()).load(elements.getImageUrl()).transform(new CircleTransform()).into(teacherImage);
            }
    
            @Override
            public void onClick(View v) {
    
                //itemAdapterListener.itemClicked(v,current);
            }
        }
    }
    
      

    我想要的是:为什么我的RecyclerVIew的子项是布局容器?   我需要有人帮助我谢谢

0 个答案:

没有答案