RecyclerView项是可单击但不可见的 [英] RecyclerView items are clickable but invisible

查看:51
本文介绍了RecyclerView项是可单击但不可见的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力解决这个问题. 真的很奇怪 我有一个具有以下结构的片段.

I have been struggling a few days with this problem. It is really strange. I have a fragment with the following structure.

ScrollView
    ConstraintLayout
        TextView1
        TwoWayView1
        TextView2
        TwoWayView2

TwoWayView(RecyclerView库)中正在填充这些项目, 我添加了onItemClickListener来测试项目是否确实存在. 因此,当我在应该放置项目的区域中点击空白区域时.它为我提供了该物品的正确位置和名称.但是该项目不可见.

The TwoWayView (RecyclerView library) is being populated with the items, I added a onItemClickListener to test if the items is indeed there. So when I tap on an empty space in the area where the item should be. It gives me the correct position and name of that item. But the item is not visible.

我真的希望有人能帮助我. 这是我的代码.

I really hope someone can help me. Here is my code.

片段事件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:context="EventsFragment">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tvHeaderSocialEvents"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="32dp"
        android:text="Social Events"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintHorizontal_bias="0.033"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1" />

    <org.lucasr.twowayview.widget.TwoWayView
        android:id="@+id/lvSocialEvents"
        style="@style/TwoWayView"
        android:layout_width="0dp"
        android:layout_height="260dp"
        android:layout_marginTop="8dp"
        android:clipChildren="true"
        android:clipToPadding="false"
        android:drawSelectorOnTop="false"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvHeaderSocialEvents"
        tools:context=".activities.MenuGuillotineActivity"
        tools:layout_conversion_absoluteHeight="80dp"
        tools:layout_conversion_absoluteWidth="395dp"
        tools:layout_conversion_absoluteX="8dp"
        tools:layout_conversion_absoluteY="68dp" />


    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="846dp"
        android:text="temp"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="175dp" />

    <TextView
        android:id="@+id/tvHeaderSportEvents"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="32dp"
        android:text="Sport Events"
        android:textSize="24sp"
        app:layout_constraintHorizontal_bias="0.027"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/lvSocialEvents" />

    <org.lucasr.twowayview.widget.TwoWayView
        android:id="@+id/lvSportEvents"
        style="@style/TwoWayView"
        android:layout_width="0dp"
        android:layout_height="80dp"
        android:layout_marginTop="8dp"
        android:drawSelectorOnTop="false"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvHeaderSportEvents"
        tools:LayoutManager="TwoWayLayoutManager"
        tools:context=".activities.MenuGuillotineActivity"
        tools:layout_conversion_absoluteHeight="80dp"
        tools:layout_conversion_absoluteWidth="395dp"
        tools:layout_conversion_absoluteX="8dp"
        tools:layout_conversion_absoluteY="68dp" />

    <TextView
        android:id="@+id/tvHeaderOfficialEvents"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="32dp"
        android:text="Official Events"
        android:textSize="24sp"
        app:layout_constraintHorizontal_bias="0.025"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/lvSportEvents" />

    <org.lucasr.twowayview.widget.TwoWayView
        android:id="@+id/lvOfficialEvents"
        style="@style/TwoWayView"
        android:layout_width="0dp"
        android:layout_height="80dp"
        android:layout_marginTop="8dp"
        android:drawSelectorOnTop="false"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvHeaderOfficialEvents"
        tools:LayoutManager="TwoWayLayoutManager"
        tools:context=".activities.MenuGuillotineActivity"
        tools:layout_conversion_absoluteHeight="80dp"
        tools:layout_conversion_absoluteWidth="395dp"
        tools:layout_conversion_absoluteX="8dp"
        tools:layout_conversion_absoluteY="68dp" />
    </android.support.constraint.ConstraintLayout>
</ScrollView>

EventsFragment

public class EventsFragment extends Fragment {

ArrayList<EventClass> eventClassArrayList;
RecyclerView.Adapter eventClassEventsAdapter;
TwoWayView lvSocialEvents;
View masterView;
TwoWayView lvSportEvents;
TwoWayView lvOfficialEvents;
StudentClass studentClass;
String studRank;
Context context;



public EventsFragment() {
    // Required empty public constructor
}

private void createHeadings(View view) {
    TextView lvHeaderSocialEvents = (TextView) view.findViewById(R.id.tvHeaderSocialEvents);
    lvHeaderSocialEvents.setTypeface(EasyFonts.caviarDreamsBold(context));
    TextView lvHeaderSportEvents = (TextView) view.findViewById(R.id.tvHeaderSportEvents);
    lvHeaderSportEvents.setTypeface(EasyFonts.caviarDreamsBold(context));
    TextView lvHeaderOfficialEvents = (TextView) view.findViewById(R.id.tvHeaderOfficialEvents);
    lvHeaderOfficialEvents.setTypeface(EasyFonts.caviarDreamsBold(context));

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_events, container, false);
    masterView = view;


    // Get Application Context
    context = getContext().getApplicationContext();
    Fresco.initialize(context);


    // Assign components
    lvSocialEvents = (TwoWayView) view.findViewById(R.id.lvSocialEvents);
    lvSocialEvents.setLayoutManager(new ListLayoutManager(context, TwoWayLayoutManager.Orientation.HORIZONTAL));


    lvSocialEvents.setHasFixedSize(true);
    lvSportEvents = (TwoWayView) view.findViewById(R.id.lvSportEvents);
    lvSportEvents.setLayoutManager(new ListLayoutManager(context, TwoWayLayoutManager.Orientation.HORIZONTAL));
    lvSportEvents.setHasFixedSize(true);
    lvOfficialEvents = (TwoWayView) view.findViewById(R.id.lvOfficialEvents);
    lvOfficialEvents.setLayoutManager(new ListLayoutManager(context, TwoWayLayoutManager.Orientation.HORIZONTAL));
    lvOfficialEvents.setHasFixedSize(true);

    createHeadings(view);

    // Get global data
    SharedPreferences global =  context.getSharedPreferences("globalData", MODE_PRIVATE);
    if (global.contains("student")) {
        //Get json from SP and decode json to EmployeeClass.
        SharedPreferences shared =  context.getSharedPreferences("studentData", MODE_PRIVATE);
        Gson gson = new Gson();
        String json = shared.getString("studentObject", "");
        studentClass = gson.fromJson(json, StudentClass.class);
        studRank = studentClass.rank;


    } else {
        studRank = "0";
    }


    // Get all the events from the Web server
    StringRequest stringRequest = new StringRequest(Request.Method.POST, getResources().getString(R.string.url_event), new Response.Listener<String>() {
        @Override
        public void onResponse(String output) {
            if (!output.isEmpty()) {
                ArrayList<CommunicationClass> data = new JsonConverter<CommunicationClass>().toArrayList(output, CommunicationClass.class);
                CommunicationClass comm = data.get(0);

                if (comm.code.equals("0")) {

                    // Create list for Events
                    eventClassArrayList = new JsonConverter<EventClass>().toArrayList(comm.info, EventClass.class);


                    // Create and add events to EventsAdapter and add to TwoWayView
                    eventClassEventsAdapter = new EventsAdapter(context,eventClassArrayList);

                    lvSocialEvents.setAdapter(eventClassEventsAdapter);
                    lvSportEvents.setAdapter(eventClassEventsAdapter);
                    lvOfficialEvents.setAdapter(eventClassEventsAdapter);

                    eventClassEventsAdapter.notifyDataSetChanged();

                } else {
                    ErrorHandling.makeText(context,comm.code, false).show();
                }
            } else {

            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            ErrorHandling.makeText(context, volleyError).showVolley();
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {

            //POST data for PHP Script
            HashMap<String, String> postDataEvents = new HashMap<String, String>();
            postDataEvents.put("event", "true");
            if (studRank.equals("0")) {
                postDataEvents.put("token", "public");
            } else {
                postDataEvents.put("token", studentClass.token);
            }
            return postDataEvents;
        }
    };

    MySingleton.getInstance(context).addToRequestQueue(stringRequest);

    com.example.howldevelopment.bcofficial.interfaces.ItemClickSupport itemClickSupport = com.example.howldevelopment.bcofficial.interfaces.ItemClickSupport.addTo(lvSocialEvents);
    itemClickSupport.setOnItemClickListener(new com.example.howldevelopment.bcofficial.interfaces.ItemClickSupport.OnItemClickListener() {
        @Override
        public void onItemClicked(RecyclerView recyclerView, int position, View v) {
            Toast.makeText(context, eventClassArrayList.get(position).name, Toast.LENGTH_SHORT).show();
        }
    });

    return view;
}

}

EventsAdapter

public class EventsAdapter extends    RecyclerView.Adapter<EventsAdapter.EventViewHolder> {

private Context context;
private ArrayList<EventClass> events;
private LayoutInflater mInflater;

public EventsAdapter(Context context,ArrayList<EventClass> events) {
    mInflater = LayoutInflater.from(context);
    this.context = context;
    this.events = events;
}

@Override
public EventViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = mInflater.inflate(R.layout.event_item_1, parent, false);
    EventViewHolder holder = new EventViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(EventViewHolder holder, int position) {
    Toast.makeText(context, events.get(position).name, Toast.LENGTH_SHORT).show();
    holder.tvName.setText(events.get(position).name);
    holder.tvDate.setText(events.get(position).date);
    holder.tvCost.setText(events.get(position).cost);
    holder.tvInterested.setText(events.get(position).interested);
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}

@Override
public int getItemCount() {
    return events.size();
}

class EventViewHolder extends RecyclerView.ViewHolder {
    TextView tvName;
    TextView tvDate;
    TextView tvCost;
    TextView tvInterested;
    ImageView imgPoster;

    public EventViewHolder(View itemView) {
        super(itemView);
        tvName = (TextView) itemView.findViewById(R.id.tvEventName);
        tvDate = (TextView) itemView.findViewById(R.id.tvEventDate);
        tvCost = (TextView) itemView.findViewById(R.id.tvEventPrice);
        tvInterested = (TextView) itemView.findViewById(R.id.tvEventInterested);
    }
}

}

它确实在几个小时前工作了.但是在EventsAdapter中添加了更多组件和所需的代码之后.它停止工作了.并尝试返回正常运行的版本,但没有成功

It did work a few hours ago. But after added more components and the required code in the EventsAdapter. It stopped working. And try to go back to the working build, But no success

我也得到了E/RecyclerView:没有连接适配器;跳过布局错误

I also get E/RecyclerView: No adapter attached; skipping layout error

推荐答案

Android Studio显示我的字体颜色是灰色,而在现实生活中它实际上是白色(或我的口音颜色).

Android Studio showed that my font color was the greyish color, while in real life it was actually white (or my Accent Color).

我刚刚更改了文本字体的颜色,并且一切运行正常.

I just changed the color of the text font and everything was working perfectly.

这篇关于RecyclerView项是可单击但不可见的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆