-
-
Save woshidan/a7ca1dc327f4972f22d2 to your computer and use it in GitHub Desktop.
RecyclerViewで行ごとのレイアウト切り替えがやりたい
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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" android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> | |
<android.support.v7.widget.RecyclerView | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:id="@+id/recycler_view" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
app:layoutManager="android.support.v7.widget.LinearLayoutManager" | |
/> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.woshidan.recyclerviewholderex; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import com.example.woshidan.recyclerviewholderex.adapter.SampleAdapter; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); | |
recyclerView.setHasFixedSize(true); | |
LinearLayoutManager layoutManager = new LinearLayoutManager(this); | |
recyclerView.setLayoutManager(layoutManager); | |
List<SampleItem> data = new ArrayList<>(); | |
SampleItem item = null; | |
for (int i = 0; i < 30; i++) { | |
switch(i % 10) { | |
case 0: | |
item = new SampleItem(1, "Data" + i); | |
break; | |
case 1: | |
item = new SampleItem(1, "Data" + i); | |
break; | |
case 2: | |
item = new SampleItem(2, "Data" + i); | |
break; | |
case 3: | |
item = new SampleItem(3, "Data" + i); | |
break; | |
case 4: | |
item = new SampleItem(2, "Data" + i); | |
break; | |
case 5: | |
item = new SampleItem(1, "Data" + i); | |
break; | |
case 6: | |
item = new SampleItem(2, "Data" + i); | |
break; | |
case 7: | |
item = new SampleItem(2, "Data" + i); | |
break; | |
case 8: | |
item = new SampleItem(3, "Data" + i); | |
break; | |
case 9: | |
item = new SampleItem(1, "Data" + i); | |
break; | |
} | |
data.add(item); | |
} | |
recyclerView.setAdapter(new SampleAdapter(this, data)); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:padding="3dp" | |
android:background="#FF0000" <!-- 2 -> #00FF00 3 -> #0000FF --> | |
android:text="New Text" | |
android:id="@+id/textView1" /> <!-- textViewn --> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:padding="3dp" | |
android:background="#00FF00" | |
android:text="New Text" | |
android:id="@+id/textView2" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:padding="3dp" | |
android:background="#0000FF" | |
android:text="New Text" | |
android:id="@+id/textView3" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.woshidan.recyclerviewholderex.adapter; | |
import android.content.Context; | |
import android.support.v7.widget.RecyclerView; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
import com.example.woshidan.recyclerviewholderex.R; | |
import com.example.woshidan.recyclerviewholderex.SampleItem; | |
import org.w3c.dom.Text; | |
import java.util.List; | |
/** | |
* Created by yoshidanozomi on 2015/11/01. | |
*/ | |
public class SampleAdapter extends RecyclerView.Adapter<SampleAdapter.ViewHolder> { | |
private final LayoutInflater inflater; | |
private final List<SampleItem> data; | |
public SampleAdapter(Context context, List<SampleItem> data) { | |
this.inflater = LayoutInflater.from(context); | |
this.data = data; | |
} | |
@Override | |
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
switch (viewType) { | |
case 1: | |
return new Sample1ItemViewHolder(inflater.inflate(Sample1ItemViewHolder.LAYOUT_ID, parent, false)); | |
case 2: | |
return new Sample2ItemViewHolder(inflater.inflate(Sample2ItemViewHolder.LAYOUT_ID, parent, false)); | |
case 3: | |
return new Sample3ItemViewHolder(inflater.inflate(Sample3ItemViewHolder.LAYOUT_ID, parent, false)); | |
} | |
return null; | |
} | |
@Override | |
public void onBindViewHolder(ViewHolder holder, int position) { | |
switch (getItemViewType(position)) { | |
case 1: | |
((Sample1ItemViewHolder)holder).onBindItemViewHolder(data.get(position)); | |
break; | |
case 2: | |
((Sample2ItemViewHolder)holder).onBindItemViewHolder(data.get(position)); | |
break; | |
case 3: | |
((Sample3ItemViewHolder)holder).onBindItemViewHolder(data.get(position)); | |
break; | |
} | |
} | |
public int getItemViewType(int position) { | |
return data.get(position).getCategory(); | |
} | |
@Override | |
public int getItemCount() { | |
return data.size(); | |
} | |
public class ViewHolder extends RecyclerView.ViewHolder { | |
public ViewHolder(View itemView) { | |
super(itemView); | |
} | |
} | |
public class Sample1ItemViewHolder extends ViewHolder { | |
public static final int LAYOUT_ID = R.layout.sample_1_item_view; | |
public final TextView textView; | |
public Sample1ItemViewHolder(View itemView) { | |
super(itemView); | |
textView = (TextView) itemView.findViewById(R.id.textView1); | |
} | |
public void onBindItemViewHolder(SampleItem data) { | |
this.textView.setText(data.getText()); | |
} | |
} | |
public class Sample2ItemViewHolder extends ViewHolder { | |
public static final int LAYOUT_ID = R.layout.sample_2_item_view; | |
public final TextView textView; | |
public Sample2ItemViewHolder(View itemView) { | |
super(itemView); | |
textView = (TextView) itemView.findViewById(R.id.textView2); | |
} | |
public void onBindItemViewHolder(SampleItem data) { | |
this.textView.setText(data.getText()); | |
} | |
} | |
public class Sample3ItemViewHolder extends ViewHolder { | |
public static final int LAYOUT_ID = R.layout.sample_3_item_view; | |
public final TextView textView; | |
public Sample3ItemViewHolder(View itemView) { | |
super(itemView); | |
textView = (TextView) itemView.findViewById(R.id.textView3); | |
} | |
public void onBindItemViewHolder(SampleItem data) { | |
this.textView.setText(data.getText()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.woshidan.recyclerviewholderex; | |
/** | |
* Created by yoshidanozomi on 2015/11/01. | |
*/ | |
public class SampleItem { | |
private String text; | |
private int category; | |
public SampleItem(int category, String text) { | |
this.category = category; | |
this.text = text; | |
} | |
public String getText() { | |
return this.text; | |
} | |
public int getCategory() { | |
return this.category; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment