如何在MainActivity中使用recyclerview显示片段

时间:2017-12-15 11:20:21

标签: java android android-fragments android-recyclerview recycler-adapter

我有一个片段,其中我显示的listitem包含两个textview和3个图像按钮通过recyclerview。我的片段被添加到主要活动中,但是当应用程序启动时,recyler视图不可见

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout
    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:id="@+id/placeholder"
    tools:context="com.example.anonymous.note_taking_app.MainActivity">

</FrameLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="16dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="TextView"
        app:layout_constraintBottom_toTopOf="@+id/detail"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.025"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.615" />

    <ImageButton
        android:id="@+id/delete"
        android:layout_width="44dp"
        android:layout_height="48dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.272"
        app:layout_constraintStart_toEndOf="@+id/edit"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.017"
        app:srcCompat="@drawable/delete" />

    <ImageButton
        android:id="@+id/edit"
        android:layout_width="51dp"
        android:layout_height="48dp"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="12dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/share"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.017"
        app:srcCompat="@drawable/edit"
        android:layout_marginLeft="12dp" />

    <ImageButton
        android:id="@+id/share"
        android:layout_width="47dp"
        android:layout_height="50dp"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="212dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.017"
        app:srcCompat="@drawable/share"
        android:layout_marginLeft="212dp" />

    <TextView
        android:id="@+id/detail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="436dp"
        android:layout_marginEnd="308dp"
        android:layout_marginStart="8dp"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.8"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginRight="308dp"
        android:layout_marginLeft="8dp" />

</android.support.constraint.ConstraintLayout>

list_fragment.java

package com.example.anonymous.note_taking_app;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;

import java.util.ArrayList;

/**
 * Created by Anonymous on 12/13/2017.
 */

public class ListFragment extends Fragment
{
    RecyclerView recyClerView;
    RecyclerView.Adapter adapter;
    RecyclerView.LayoutManager layoutManager;
    ArrayList<ListItem> it;

    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
    {
        it = new ArrayList<>();
        View view = inflater.inflate(R.layout.fragment_list, parent, false);

        for(int i=0;i<=20;i++)
        {
             ListItem t = new ListItem("Umair","qwertyuio",R.drawable.share,R.drawable.edit,R.drawable.delete) ;
             it.add(t);
        }


        recyClerView=(RecyclerView) view.findViewById(R.id.recycle);
        com.example.anonymous.note_taking_app.ListAdapter l = new com.example.anonymous.note_taking_app.ListAdapter(it);
        recyClerView.setAdapter(l);
        recyClerView.setLayoutManager(layoutManager);

        return view;

    }
}

ListAdapter.java

package com.example.anonymous.note_taking_app;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by Anonymous on 12/13/2017.
 */

public class ListAdapter extends RecyclerView.Adapter
{


    private ArrayList<ListItem> i;
    Context context;

    public ListAdapter(ArrayList<ListItem> i)
    {
        this.i=i;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
        holderclas h = new holderclas(view);
        return h;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        ((holderclas) holder).bindview(position);
    }

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




   /* @Override
    public ListAdapter.holderclas onCreateViewHolder(ViewGroup parent, int viewType)
    {

        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
        holderclas h = new holderclas(view);
        return h;
    }

    @Override
    public void onBindViewHolder(ListAdapter.holderclas holder, int position)
    {
        ListItem it = i.get(position);
        holder.title.setText(it.getNoteTitle());
        holder.details.setText(it.getNoteDetail());
        holder.shareimagebutton.setImageResource(it.getShareImageButton());
        holder.editimagebutton.setImageResource(it.getShareImageButton());
        holder.deleteimagebutton.setImageResource(it.getDeleteImageButton());
    }

    @Override
    public int getItemCount() {
        return i.size();
    }*/

    public class holderclas extends RecyclerView.ViewHolder
    {

        TextView title;
        TextView details;
        ImageButton shareimagebutton;
        ImageButton editimagebutton;
        ImageButton deleteimagebutton;

        public holderclas(View itemView)
        {

            super(itemView);
            title=(TextView) itemView.findViewById(R.id.title);
            details=(TextView) itemView.findViewById(R.id.detail);
            shareimagebutton=(ImageButton) itemView.findViewById(R.id.share);
            editimagebutton=(ImageButton) itemView.findViewById(R.id.edit);
            deleteimagebutton=(ImageButton) itemView.findViewById(R.id.delete);

        }

        public void bindview(int position)
        {
            ListItem it = i.get(position);
            title.setText(it.getNoteTitle());
            details.setText(it.getNoteDetail());
            shareimagebutton.setImageResource(it.getShareImageButton());
            editimagebutton.setImageResource(it.getShareImageButton());
            deleteimagebutton.setImageResource(it.getDeleteImageButton());
        }
    }
}

MainActivity.java

package com.example.anonymous.note_taking_app;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListFragment f= new ListFragment();
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.placeholder,f);
       // ft.add(R.id.placeholder,f);
        ft.commit();
    }
}

fragment_list.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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.support.v7.widget.RecyclerView
        android:layout_width="368dp"
        android:layout_height="495dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:id="@+id/recycle"/>

</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:2)

您忘记在layoutManager

中初始化ListFragment
  

如果您想要垂直而不是使用此

layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
  

如果你想要 HORIZONTAL 而不是使用这个linearLayoutManager

layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
  

编辑让你的 list_item.xml hight到android:layout_height =&#34; wrap_content&#34;如下面的代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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="wrap_content">