在FRAGMENT中创建RecyclerView

时间:2017-05-15 01:10:18

标签: android android-recyclerview

我想在Fragment中的recyclerView中创建这个UI,以及如何从其他片段调用   它有TextView和ImageView。我在Adapter ViewHolder Line中收到错误。      我做到了这一点。任何人都可以帮助我吗?

enter image description here

ModelAdapter.java

   public class ModelAdapter extends       RecyclerView.Adapter<ModelAdapter.MyViewHolder> {

  private List<ItemData> itemsData;
  public ImageView carBrandImg;
public TextView carBrandName;



public class MyViewHolder extends RecyclerView.ViewHolder {



    public MyViewHolder(View itemView) {
        super(itemView);
        carBrandImg = (ImageView) itemView.findViewById(R.id.carBrandImg);
        carBrandName = (TextView) itemView.findViewById(R.id.carBrandName);
    }
}





public ModelAdapter(ItemData[] itemsData) {
    this.itemsData = itemsData;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.row_model, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    ItemData brand = itemsData.get(position);
    MyViewHolder.carBrandImg.setImageResource(brand.getImg());
    MyViewHolder.carBrandName.setText(brand.getText());

}

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

ItemData.java

public class ItemData {
String text,img;


public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

public String getImg() {
    return img;
}

public void setImg(String img) {
    this.img = img;
}

public ItemData(String text, String img) {

    this.text = text;
    this.img = img;
}
}

MyFragment.java

  public class MyFragment extends Fragment {


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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View rootView = inflater.inflate(R.layout.fragment_my, container, false);
    // 1. get a reference to recyclerView
    RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.ls);

    // 2. set layoutManger
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    // this is data fro recycler view
    ItemData itemsData[] = {
            new ItemData("Indigo",R.drawable.abc),
            new ItemData("Red",R.drawable.xyz),
            new ItemData("Blue",R.drawable.pqr),
            new ItemData("Green",R.drawable.def),

    };


    // 3. create an adapter
    ModelAdapter mAdapter = new ModelAdapter(itemsData);
    // 4. set adapter
    recyclerView.setAdapter(mAdapter);
    // 5. set item animator to DefaultAnimator
    recyclerView.setItemAnimator(new DefaultItemAnimator());


    return rootView;
}
 }

fragment_my.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:id="@+id/headermodel"
    android:background="#9294a3">

    <TextView
        android:layout_width="35dp"
        android:text="1"
        android:textColor="#c2bbbb"
        android:gravity="center"
        android:textSize="30sp"
        android:layout_gravity="center"
        android:layout_marginLeft="15dp"

        android:layout_height="35dp" />

    <TextView
        android:layout_width="35dp"
        android:text="2"
        android:textColor="#fdfcfc"
        android:gravity="center"
        android:textSize="30sp"
        android:layout_gravity="center"
        android:layout_marginLeft="15dp"

        android:layout_height="35dp" />

    <TextView
        android:layout_width="220dp"
        android:layout_height="match_parent"
        android:text="Pick Your Model"
        android:textSize="20sp"
        android:paddingLeft="15dp"
        android:textColor="#faf8f8"
        android:gravity="center_vertical" />


    <TextView
        android:layout_width="35dp"
        android:text="3"
        android:textColor="#c2bbbb"
        android:gravity="center"
        android:textSize="30sp"
        android:layout_gravity="center"
        android:layout_marginLeft="15dp"

        android:layout_height="35dp" />



</LinearLayout>

<android.support.v7.widget.RecyclerView
    android:layout_width="fill_parent"
    android:id="@+id/ls"
    android:layout_below="@+id/headermodel"
    android:layout_height="fill_parent">

</android.support.v7.widget.RecyclerView>

 </RelativeLayout>

row_model.xml

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical">



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="10dp"
        android:layout_marginStart="40dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/carBrandImg"
            android:layout_width="70dp"
            android:layout_height="match_parent" />

        <TextView
            android:id="@+id/carBrandName"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:paddingLeft="20dp"
            android:text="Renault"
            android:textColor="#2e2727"
            android:textSize="20sp" />
    </LinearLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

这是代码:)随意玩它。

using System.Collections.Generic;
using Android.Content;
using Android.OS;
using Android.Views;
using Android.Widget;
using SupportFragment = Android.Support.V4.App.Fragment;
using Android.Support.V7.Widget;
using Android.Util;
using Newtonsoft.Json;
using BusinessLogic;
using BusinessObject;
using System;
using Android.Graphics;

namespace RBOS_2._0._1
{
  class Fragment_Cashflow : SupportFragment
  {
    //local variable declaration

    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Inflate recyclerView for this Fragment
        RecyclerView recyclerView = inflater.Inflate(Resource.Layout.RecyclerView, container, false) as RecyclerView;

        // Start Recycler View
        SetUpRecyclerView(recyclerView);

        return recyclerView;
    }

    /// <summary>
    /// Setting up recyclerView
    /// </summary>
    /// <param name="recyclerView">recyclerView</param>
    private void SetUpRecyclerView(RecyclerView recyclerView)
    {
        recyclerView.SetLayoutManager(new LinearLayoutManager(recyclerView.Context));
        recyclerView.SetAdapter(new SimpleStringRecyclerViewAdapter()); 
        //this depends on how you use it (with or without arguments)
        //it is for your adapter

        recyclerView.SetItemClickListener((rv, position, view) =>
        {
        });
    }

    /// <summary>
    /// Implements the three required method overrides
    /// </summary>
    public class SimpleStringRecyclerViewAdapter : RecyclerView.Adapter
    {
        private readonly TypedValue mTypedValue = new TypedValue();

        //this is a sample variable declaration i nside adapter
        private int mBackground;
        private List<string> mDate;
        private List<string> mStatus;
        //until here

        //constructor
        public SimpleStringRecyclerViewAdapter()
        {
            context.Theme.ResolveAttribute(Resource.Attribute.selectableItemBackground, mTypedValue, true);
            mBackground = mTypedValue.ResourceId;
        }

        /// <summary>
        /// Gets the count of Values
        /// </summary>
        public override int ItemCount
        {
            get { return mDate.Count; }
        }

        /// <summary>
        /// Directly deals with the data.
        /// </summary>
        /// <param name="holder">holder</param>
        /// <param name="position">position</param>
        public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
        {
            var simpleHolder = holder as SimpleViewHolder;

            simpleHolder.mTxtViewDate.Text = DateTime.Now;
            simpleHolder.mTxtViewStatus.Text = "Status ";      
            //you can use the local variables if you want.             
        }

        /// <summary>
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="viewType"></param>
        /// <returns></returns>
        public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
        {
            View view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.YourLayout, parent, false);
            view.SetBackgroundResource(mBackground);

            return new SimpleViewHolder(view);
        }
    }

    /// <summary>
    /// Locates, holds and caches the view references from the resources.
    /// </summary>
    public class SimpleViewHolder : RecyclerView.ViewHolder
    {
        public readonly View mView;
        public readonly TextView mTxtViewDate;
        public readonly TextView mTxtViewStatus;

        /// <summary>
        /// Finding View and TextView and creating an Instance in C#
        /// </summary>
        /// <param name="view">view</param>
        public SimpleViewHolder(View view) : base(view)
        {
            mView = view;
            mTxtViewDate = view.FindViewById<TextView>(Resource.Id.entry_date);
            mTxtViewStatus = view.FindViewById<TextView>(Resource.Id.entry_status);
        }
    }
  }
}