简体   繁体   English

RecyclerView项可单击但不可见

[英]RecyclerView items are clickable but invisible

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

The TwoWayView (RecyclerView library) is being populated with the items, I added a onItemClickListener to test if the items is indeed there. TwoWayView(RecyclerView库)中正在填充项目,我添加了onItemClickListener来测试项目是否确实存在。 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. 这是我的代码。

fragment_events fragment_events

<?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 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 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);
    }
}

}

It did work a few hours ago. 它确实在几个小时前工作了。 But after added more components and the required code in the EventsAdapter. 但是在EventsAdapter中添加了更多组件和所需的代码之后。 It stopped working. 它停止工作了。 And try to go back to the working build, But no success 并尝试返回正常工作状态,但没有成功

I also get E/RecyclerView: No adapter attached; 我也得到了E / RecyclerView:没有连接适配器; skipping layout error 跳过布局错误

Android Studio showed that my font color was the greyish color, while in real life it was actually white (or my Accent Color). Android Studio显示我的字体颜色是灰色,而在现实生活中它实际上是白色(或我的口音颜色)。

I just changed the color of the text font and everything was working perfectly. 我只是更改了文本字体的颜色,所以一切运行正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM