2017-04-04 104 views
-1

我想在AppCompatActivity的ViewPager中的片段中創建一個ListView。在AppCompatActivity中,所有視圖元素都被封裝在CoordinatorLayout中。因爲我使用了CoordinatorLayout。我必須使用RecylerView我試圖遵循developer.android.com的培訓。是否有可能將此RecyclerView代碼更改爲RecyclerViewFragment代碼?如果可能的話,請幫我改變它。Android - RecyclerView到RecyclerViewFragment

感謝

import android.content.Context; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.content.ContextCompat; 
import android.support.v4.app.Fragment; 
import android.support.v7.widget.GridLayoutManager; 
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.ImageView; 
import android.widget.TextView; 
import java.util.Collections; 
import java.util.List; 

public class AdapterFish extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 

    private Context context; 
    private LayoutInflater inflater; 
    List<DataFish> data= Collections.emptyList(); 
    DataFish current; 
    int currentPos=0; 

    // create constructor to innitilize context and data sent from MainActivity 
    public AdapterFish(Context context, List<DataFish> data){ 
     this.context=context; 
     inflater= LayoutInflater.from(context); 
     this.data=data; 
    } 

    // Inflate the layout when viewholder created 
    @Override 
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view=inflater.inflate(R.layout.tab2, parent,false); 
     MyHolder holder=new MyHolder(view); 
     return holder; 
    } 

    // Bind data 
    @Override 
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 

     // Get current position of item in recyclerview to bind data and assign values from list 
     MyHolder myHolder= (MyHolder) holder; 
     DataFish current=data.get(position); 
     myHolder.textmatkul.setText(current.matkul); 
     myHolder.textwaktu_batal.setText("Tanggal Batal: " + current.waktu_batal); 
     myHolder.textwaktu_pengganti.setText("Tanggal Pengganti: " + current.waktu_pengganti); 
     myHolder.textdosen.setText("Nama Dosen: " + current.dosen); 
     myHolder.textruang.setText("Ruang: " + current.ruang); 
     myHolder.textalasan.setText("Alasan: " + current.alasan); 
    } 

    // return total item from List 
    @Override 
    public int getItemCount() { 
     return data.size(); 
    } 


    class MyHolder extends RecyclerView.ViewHolder{ 

     TextView textmatkul; 
     TextView textwaktu_batal; 
     TextView textwaktu_pengganti; 
     TextView textdosen; 
     TextView textruang; 
     TextView textalasan; 

     // create constructor to get widget reference 
     public MyHolder(View itemView) { 
      super(itemView); 
      textmatkul= (TextView) itemView.findViewById(R.id.textmatkul); 
      textwaktu_batal = (TextView) itemView.findViewById(R.id.textwaktu_batal); 
      textwaktu_pengganti = (TextView) itemView.findViewById(R.id.textwaktu_pengganti); 
      textdosen = (TextView) itemView.findViewById(R.id.textdosen); 
      textalasan = (TextView) itemView.findViewById(R.id.textalasan); 
      textruang = (TextView) itemView.findViewById(R.id.textruang); 
     } 

    } 
} 

回答

0

這是[enter link description here

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v7.widget.GridLayoutManager; 
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.RadioButton; 

/** 
* Demonstrates the use of {@link RecyclerView} with a {@link LinearLayoutManager} and a 
* {@link GridLayoutManager}. 
*/ 
public class RecyclerViewFragment extends Fragment { 

    private static final String TAG = "RecyclerViewFragment"; 
    private static final String KEY_LAYOUT_MANAGER = "layoutManager"; 
    private static final int SPAN_COUNT = 2; 
    private static final int DATASET_COUNT = 60; 

    private enum LayoutManagerType { 
     GRID_LAYOUT_MANAGER, 
     LINEAR_LAYOUT_MANAGER 
    } 

    protected LayoutManagerType mCurrentLayoutManagerType; 

    protected RadioButton mLinearLayoutRadioButton; 
    protected RadioButton mGridLayoutRadioButton; 

    protected RecyclerView mRecyclerView; 
    protected CustomAdapter mAdapter; 
    protected RecyclerView.LayoutManager mLayoutManager; 
    protected String[] mDataset; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // Initialize dataset, this data would usually come from a local content provider or 
     // remote server. 
     initDataset(); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.recycler_view_frag, container, false); 
     rootView.setTag(TAG); 

     mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView); 

     // LinearLayoutManager is used here, this will layout the elements in a similar fashion 
     // to the way ListView would layout elements. The RecyclerView.LayoutManager defines how 
     // elements are laid out. 
     mLayoutManager = new LinearLayoutManager(getActivity()); 

     mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER; 

     if (savedInstanceState != null) { 
      // Restore saved layout manager type. 
      mCurrentLayoutManagerType = (LayoutManagerType) savedInstanceState 
        .getSerializable(KEY_LAYOUT_MANAGER); 
     } 
     setRecyclerViewLayoutManager(mCurrentLayoutManagerType); 

     mAdapter = new CustomAdapter(mDataset); 
     // Set CustomAdapter as the adapter for RecyclerView. 
     mRecyclerView.setAdapter(mAdapter); 

     mLinearLayoutRadioButton = (RadioButton) rootView.findViewById(R.id.linear_layout_rb); 
     mLinearLayoutRadioButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       setRecyclerViewLayoutManager(LayoutManagerType.LINEAR_LAYOUT_MANAGER); 
      } 
     }); 

     mGridLayoutRadioButton = (RadioButton) rootView.findViewById(R.id.grid_layout_rb); 
     mGridLayoutRadioButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       setRecyclerViewLayoutManager(LayoutManagerType.GRID_LAYOUT_MANAGER); 
      } 
     }); 

     return rootView; 
    } 

    /** 
    * Set RecyclerView's LayoutManager to the one given. 
    * 
    * @param layoutManagerType Type of layout manager to switch to. 
    */ 
    public void setRecyclerViewLayoutManager(LayoutManagerType layoutManagerType) { 
     int scrollPosition = 0; 

     // If a layout manager has already been set, get current scroll position. 
     if (mRecyclerView.getLayoutManager() != null) { 
      scrollPosition = ((LinearLayoutManager) mRecyclerView.getLayoutManager()) 
        .findFirstCompletelyVisibleItemPosition(); 
     } 

     switch (layoutManagerType) { 
      case GRID_LAYOUT_MANAGER: 
       mLayoutManager = new GridLayoutManager(getActivity(), SPAN_COUNT); 
       mCurrentLayoutManagerType = LayoutManagerType.GRID_LAYOUT_MANAGER; 
       break; 
      case LINEAR_LAYOUT_MANAGER: 
       mLayoutManager = new LinearLayoutManager(getActivity()); 
       mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER; 
       break; 
      default: 
       mLayoutManager = new LinearLayoutManager(getActivity()); 
       mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER; 
     } 

     mRecyclerView.setLayoutManager(mLayoutManager); 
     mRecyclerView.scrollToPosition(scrollPosition); 
    } 

    @Override 
    public void onSaveInstanceState(Bundle savedInstanceState) { 
     // Save currently selected layout manager. 
     savedInstanceState.putSerializable(KEY_LAYOUT_MANAGER, mCurrentLayoutManagerType); 
     super.onSaveInstanceState(savedInstanceState); 
    } 

    /** 
    * Generates Strings for RecyclerView's adapter. This data would usually come 
    * from a local content provider or remote server. 
    */ 
    private void initDataset() { 
     mDataset = new String[DATASET_COUNT]; 
     for (int i = 0; i < DATASET_COUNT; i++) { 
      mDataset[i] = "This is element #" + i; 
     } 
    } 
} 

1含回收站視圖中的片段的例子:

所以,你需要的片段recyclerview佈局,更改ID,添加您的魚ArrayList並更改此代碼

mAdapter = new CustomAdapter(mDataset);

mAdapter = new AdapterFish(getContext(), fishes);

然後,你必須包含recyclerView

片段