2017-01-27 11 views
-1

フラグメント内にrecyclerviewを追加する方法は多々あります。私はアンドロイドのために新しいです。私のアンドロイドには5つのフラグメントがあり、これらはリサイクルビューを追加する必要があります。ここに私のコードはNotificationItem.javaフラグメントにリサイクラビューを追加する方法

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/notification_item_root" 
    android:layout_width="match_parent" 
    android:layout_height="56dp" 
    android:gravity="center_vertical|left" 
    android:orientation="horizontal" 
    android:paddingLeft="16dp"> 

    <de.hdodenhof.circleimageview.CircleImageView 
     android:id="@+id/notification_item_img" 
     android:layout_width="36dp" 
     android:layout_height="36dp" 
     android:src="@android:drawable/ic_input_get" /> 

    <TextView 
     android:id="@+id/notification_item_text" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:paddingLeft="8dp" 
     android:text="Test Testre" /> 

</LinearLayout> 

notification_Fragment.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

<android.support.v7.app.AlertController.RecycleListView 
    android:id="@+id/notification_list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

</LinearLayout> 

notification_item.xml

ある

public class NotificationItem { 
    private String title; 
    private int imageResId; 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public int getImageResId() { 
     return imageResId; 
    } 

    public void setImageResId(int imageResId) { 
     this.imageResId = imageResId; 
    } 
} 

NotificationData.java

public class NotificationData { 
    private static final String[] textItem = {"pathum", "sai", "charu"}; 
    private static final int[] imgItem = {android.R.drawable.ic_popup_reminder, android.R.drawable.ic_menu_add, android.R.drawable.ic_menu_delete}; 

    public static List<NotificationItem> getListData() { 
     List<NotificationItem> data = new ArrayList<>(); 

     for (int x = 0; x < 4; x++) { 
      for (int i = 0; i < textItem.length && i < imgItem.length; i++) { 
       NotificationItem item = new NotificationItem(); 
       item.setImageResId(imgItem[i]); 
       item.setTitle(textItem[i]); 
       data.add(item); 
      } 
     } 

     return data; 
    } 

} 

NotificationAdapter.java

public class NotificationAdapter extends RecyclerView.Adapter<NotificationAdapter.NotificationHolder> { 

    private List<NotificationItem> listData; 
    private LayoutInflater inflater; 

    public NotificationAdapter(List<NotificationItem> listData, Context c) { 

     this.inflater = LayoutInflater.from(c); 
     this.listData = listData; 
    } 

    @Override 
    public NotificationHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = inflater.inflate(R.layout.notification_item,parent,false); 
     return new NotificationHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(NotificationHolder holder, int position) { 
     NotificationItem item = listData.get(position); 
     holder.title.setText(item.getTitle()); 
     holder.icon.setImageResource(item.getImageResId()); 
    } 

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

    class NotificationHolder extends RecyclerView.ViewHolder { 

     private TextView title; 
     private CircleImageView icon; 
     private View container; 

     public NotificationHolder(View itemView) { 
      super(itemView); 

      title = (TextView) itemView.findViewById(R.id.notification_item_text); 
      icon = (CircleImageView) itemView.findViewById(R.id.notification_item_img); 
      container = itemView.findViewById(R.id.notification_item_root); 
     } 
    } 
} 

NotificationFragment.java

public class NotificationFragment extends Fragment { 

    RecyclerView recyclerView; 
    NotificationAdapter notificationAdapter; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.notification_fragment, container, false); 
    recyclerView = (RecyclerView) rootView.findViewById(R.id.notification_list); 

    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 

    notificationAdapter = new NotificationAdapter(NotificationData.getListData(),this); 
    recyclerView.setAdapter(notificationAdapter); 


    return rootView; 
} 

}

私はすぐにそれをすることができませんでしたNotificationFragment.javaNotificationAdapter.java私を助けてください。

+1

定義*私はそれを正しくすることができませんでした*問題はどこですか? – Selvin

+0

あなたのlayout.xmlプレビューであなたのrecyclerviewを見ることができますか? – Anil

+0

@AnilDSはデフォルトのrecyclerviewをプレビューしています –

答えて

0

あなたはAlertController.RecyclerListViewは、これを使用する代わりに、RecyclerViewために間違ったクラスを使用している:

ここ
<!-- A RecyclerView with some commonly used attributes --> 
<android.support.v7.widget.RecyclerView 
    android:id="@+id/my_recycler_view" 
    android:scrollbars="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

いくつかのより多くの情報です:

RecyclerView

+0

これは完了ですが、これらのファイルのヘルプが必要です。NotificationFragment.java&NotificationAdapter.java –

-1

1)が最初にアプリに依存関係を追加するには、レベルのグラデーションファイルを作成し、プロジェクトを同期させます。

compile 'com.android.support:recyclerview-v7:23.4.0' 

2)あなたのレイアウトファイル(例に行く:activity_main.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" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.examples.rehan.excluzo.Fragments.OneFragment"> 

<android.support.v7.widget.RecyclerView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:scrollbars="vertical" 
    android:id="@+id/recycler_view"> 

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

3をrecyclerviewタグを追加する)クラスmodel.javaを作成し、あなたのコードを貼り付けます。

パブリッククラスモデル{ プライベートString name、gender;

public model() { 
} 

public model(String name, String gender) { 
    this.name= name; 
    this.gender= gender; 
} 

public String getGender() { 
    return gender; 
} 

public void setGender(String gneder) { 
    this.gender= gender; 
} 


public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name= name; 
} 

}

4)あなたのリストビューに子ビューを追加するアダプタを作成するためにあなたの必要性)レイアウトフォルダitem_layout.xmlでxmlファイルを作成し、

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:focusable="true" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" 
    android:paddingTop="10dp" 
    android:paddingBottom="10dp" > 

    <TextView 
     android:id="@+id/name" 
     android:textColor="@color/title" 
     android:textSize="16dp" 
     android:textStyle="bold" 
     android:layout_alignParentTop="true" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/gender" 
     android:layout_below="@id/title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

</RelativeLayout> 

5このコードを貼り付けます。ファイルtestAdapter.javaファイルを作成し、このコードを貼り付けます。

ここにリストビューの私は2つの項目(例:名前と性別)を有することになる

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


    private List<model> productList; 
    Context context; 
    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View itemView = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.item_layout, parent, false); 

     return new MyViewHolder(itemView); 
    } 

    @Override 
    public void onBindViewHolder(MyViewHolder holder, int position) { 
     model product = productList.get(position); 
     holder.name.setText(product.getName()); 
     holder.gender.setText(product.getGender()); 
    } 

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

    public class MyViewHolder extends RecyclerView.ViewHolder { 
     public TextView name,gender; 

     public MyViewHolder(View view) { 
      super(view); 
      name = (TextView) view.findViewById(R.id.name); 
      gender = (TextView) view.findViewById(R.id.gender); 
     } 
    } 



    public testAdapter(List<model> productList, Context context) { 
     this.productList = productList; 
     this.context = context; 
    } 

} 

6)今、あなたのMainActivity.javaを後藤、このコードを貼り付け

import android.content.Context; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.DefaultItemAnimator; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.support.v7.widget.Toolbar; 
import android.view.GestureDetector; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.List; 

public class MainActivity extends AppCompatActivity { 
    private List<modle> movieList = new ArrayList<>(); 
    private RecyclerView recyclerView; 
    private testAdapter mAdapter; 

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

     recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 

     mAdapter = new testAdapter(movieList); 
     RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); 
     recyclerView.setLayoutManager(mLayoutManager); 
     recyclerView.setItemAnimator(new DefaultItemAnimator()); 
     recyclerView.setAdapter(mAdapter); 

     preparedata(); 
    } 

    private void preparedata() { 
     model movie = new model("XXX", "Male"); 
     movieList.add(movie); 

     //add the items according to your wish 
     //name,gender 
     //here i have added all items with same name and gender. you can change it 
     model movie = new model("XXX", "Male"); 
     movieList.add(movie); 

     model movie = new model("XXX", "Male"); 
     movieList.add(movie); 

     model movie = new model("XXX", "Male"); 
     movieList.add(movie); 

     model movie = new model("XXX", "Male"); 
     movieList.add(movie); 

     model movie = new model("XXX", "Male"); 
     movieList.add(movie); 


     model movie = new model("XXX", "Male"); 
     movieList.add(movie); 

     mAdapter.notifyDataSetChanged(); 
    } 
} 

・ホープ、この助けてください:)

+0

Thnx for u r answer。私はフラグメントにrecyclerviewを追加する必要があります。活動しない。私はこのことを活動で行っています。しかし、この場合私は断片の中でそれを行う必要があります。 –

関連する問題