RecyclerView项目是可点击的但不可见

时间:2017-11-02 18:54:24

标签: java android android-recyclerview

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

ScrollView
    ConstraintLayout
        TextView1
        TwoWayView1
        TextView2
        TwoWayView2

TwoWayView(RecyclerView库)正在填充项目, 我添加了一个onItemClickListener来测试项目是否确实存在。 因此,当我点击项目所在区域的空白区域时。它给了我该项目的正确位置和名称。但该项目不可见。

我真的希望有人可以帮助我。 这是我的代码。

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

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中添加了更多组件和所需代码之后。它停止了工作。并尝试回到工作版本,但没有成功

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

1 个答案:

答案 0 :(得分:1)

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

我只是改变了文字字体的颜色,一切都很完美。