2017-04-04 14 views
-1

AppCompatActivityでViewPager内のフラグメントにListViewを作成しようとしています。 AppCompatActivityでは、すべてのビュー要素がCoordinatorLayoutのwrappendです。私は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とレイアウトを必要とし、 idsを変更し、ArrayListを追加してこのコードを変更してください

mAdapter =新しいCustomAdapter(mDataset);

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

次にあなたがrecyclerView

を含む断片を持っています