2016-04-07 21 views
0

私はこのガイドを使って、過去のAndroidアプリを開発しています:Material Design Guide、Googleから。タイルフラグメント、異なるタイルで異なるコンテンツを取得するにはどうすればよいですか?

私はタイルの断片を選択することに決めましたが、問題はこれらのタイルの内容が静的であることです。タイル1の内容はタイル2,3,4などと同じです。 これを変更して、各タイルに固有のコンテンツがあるようにするにはどうすればよいですか?

ご協力いただければ幸いです!

答えて

0

通常、このタイルリスト形式で表示したいデータがあります。これはContentAdapterに渡され、各タイルを埋め込むために使用できます。すべてのコンテンツは、アダプタではなくXMLで設定されています。

あなたがitem_tileレイアウトにid属性を追加する必要がそれぞれの画像を変更する場合:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="?android:attr/selectableItemBackground" 
    android:padding="@dimen/tile_padding"> 

    <ImageView 
     !--Add the id for the ImageView--> 
     android:id="@+id/tile_image" 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/tile_height" 
     android:scaleType="centerCrop" 
     android:src="@drawable/paris" /> 

    ... 
</RelativeLayout> 

我々はを手に入れることができるように、あなたがTileContentFragmentでViewHolderクラスを変更する必要がありますitem_tileレイアウトではImageViewおよびTextViewです。

public static class ViewHolder extends RecyclerView.ViewHolder { 

    ImageView tileImage; 
    TextView tileTitle; 

    public ViewHolder(View itemView) { 
     super(itemView); 
     this.tileImage = (ImageView) itemView.findViewById(R.id.tile_image); 
     this.tileTitle = (TextView) itemView.findViewById(R.id.tile_title); 
    } 
} 

は、それからちょうど例の目的のために「こんにちは」に各タイルのタイトルを設定することができます:

public static class ContentAdapter extends RecyclerView.Adapter<ViewHolder> { 

    Other class methods... 

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

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     holder.tileTitle.setText("Hello"); 
     //If you had images for the different tile you could set them here too 
     //holder.tileImage.setImageResource([THE ID OF YOUR IMAGE HERE]) 
    } 
} 

・ホープ、このことができます。

+0

ありがとうございます!私はそれを試してみると、私がそうしたときに報告する –

+0

質問1: "TextView tileText"は "public static class ViewHolder .."で "TextView tileTitle"でないと "tileTitle = 0(TextView)tileItem.findViewById ... "public" ViewHolder(LayoutInflater ..)? /// また、 { エラー:ViewHolderクラスのコンストラクタViewHolderは、指定された型には適用できません。 が必要:ビュー が見つかりました:引数なし 理由:実際の仮引数リストの長さが異なる「 //// ERROR#2:またに」公共ViewHolder(){ 「エラー:スーパーに呼び出すと、最初のステートメントでなければなりませんコンストラクタ内 " –

+0

はい、変数名に間違いはありませんでした。エラーが発生する可能性があるので、ブラウザでそれを行っていました。 –

関連する問題