Created
January 12, 2017 07:19
-
-
Save Yosuke-Kawakami/cb18acc9db8d4d71333974d1223fa307 to your computer and use it in GitHub Desktop.
習作、Android、RecyclerCardView、部分抜粋
This file contains hidden or 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 org.example.gist.kawakami.sample.recyclercard; | |
import android.content.Context; | |
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.util.SparseArrayCompat; | |
import android.support.v7.widget.CardView; | |
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.LinearLayout; | |
import android.widget.TextView; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by y-kawakami on 2017/01/04. | |
* | |
* 内部クラスで固めて書いちゃった。 | |
* 本当は個別に書き出すべきなのだろうけれど、まあいいかなって。 | |
*/ | |
public class Frg_list extends Fragment | |
{ | |
RecyclerView mRecyclerView; | |
RecyclerView.Adapter mAdapter; | |
RecyclerView.LayoutManager mLayoutManager; | |
List<MyData> mCurrentData; | |
SparseArrayCompat<String> mHeaders; | |
@Override | |
public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstanceState) | |
{ | |
return layoutInflater.inflate(R.layout.view_list, null); | |
} | |
@Override | |
public void onActivityCreated(Bundle savedInstanceState) | |
{ | |
super.onActivityCreated(savedInstanceState); | |
mRecyclerView = (RecyclerView) getActivity().findViewById(R.id.recycler_view); | |
// use this setting to improve performance if you know that changes | |
// in content do not change the layout size of the RecyclerView | |
// 妙訳:リストの大きさがデータによって変わらない場合は true に設定することで | |
// パフォーマンスが改善される。 | |
mRecyclerView.setHasFixedSize(true); | |
// 利用可能なのは以下の三種類 | |
// | |
// - LinearLayoutManager | |
// - GridLayoutManager | |
// - StaggeredGridLayoutManager | |
// | |
mRecyclerView.setLayoutManager(mLayoutManager = new LinearLayoutManager(getActivity())); | |
// mAllData は何かしらの方法で取得して下さい。ここでは雑に親アクティビティのものを参照している | |
boolean hasNoData = (MainActivity.mAllData == null || MainActivity.mAllData.size() <= 0); | |
if(hasNoData) | |
{ | |
// リストアクティビティ的に no item の表示がしたい(本件的には余計な処理) | |
mRecyclerView.setVisibility(View.GONE); | |
LinearLayout ll = (LinearLayout) getActivity().findViewById(R.id.not_found); | |
ll.setVisibility(View.VISIBLE); | |
return; | |
} | |
int start = 1; | |
int end = MainActivity.mAllData.size(); | |
List<List<Object>> subList = MainActivity.mAllData.subList(start, end); | |
mHeaders = genHeaders(MainActivity.mAllData.get(0)); | |
mCurrentData = genList(subList); | |
mAdapter = new RecyclerCardAdapter(getActivity(), mCurrentData); | |
mRecyclerView.setAdapter(mAdapter); | |
} | |
/** | |
* | |
* @param data | |
* @return | |
*/ | |
SparseArrayCompat<String> genHeaders(List<Object> data) | |
{ | |
SparseArrayCompat<String> value = new SparseArrayCompat<String>(); | |
int i = 0; | |
for(Object row : data) | |
{ | |
value.put(i++, row.toString()); | |
} | |
return value; | |
} | |
/** | |
* | |
* @param lists | |
* @return | |
*/ | |
public List<MyData> genList(List<List<Object>> lists) | |
{ | |
ArrayList<MyData> al = new ArrayList<MyData>(); | |
for(List<Object> list : lists) | |
{ | |
al.add(new MyData(list)); | |
} | |
return al; | |
} | |
/* --- 内部クラス -------------------------------------------------- */ | |
class MyData | |
{ | |
String company_name; | |
String ae_name; | |
public MyData(List<Object> list) | |
{ | |
for(int i=0; i < mHeaders.size(); i++) | |
{ | |
String s = list.get(i).toString(); | |
switch(i) | |
{ | |
case 0: company_name = s; break; | |
case 1: ae_name = s; break; | |
} | |
} | |
} | |
public String getCompany_name() { return company_name;} | |
public String getAe_name() { return ae_name;} | |
public void setCompany_name(String company_name) { this.company_name = company_name;} | |
public void setAe_name(String ae_name) { this.ae_name = ae_name;} | |
} | |
/* --- 内部クラス -------------------------------------------------- */ | |
class RecyclerCardViewHolder extends RecyclerView.ViewHolder | |
{ | |
CardView cardView; | |
TextView company_name; | |
TextView ae_name; | |
public RecyclerCardViewHolder(View itemView) | |
{ | |
super(itemView); | |
cardView = (CardView) itemView.findViewById(R.id.card_view); | |
company_name = (TextView) itemView.findViewById(R.id.company_name); | |
ae_name = (TextView) itemView.findViewById(R.id.ae_name); | |
} | |
} | |
/* --- 内部クラス -------------------------------------------------- */ | |
class RecyclerCardAdapter extends RecyclerView.Adapter<RecyclerCardViewHolder> | |
{ | |
Context context; | |
List<MyData> list; | |
RecyclerCardAdapter(Context context, List<MyData> list) | |
{ | |
super(); | |
this.context = context; | |
this.list = list; | |
} | |
@Override | |
public RecyclerCardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) | |
{ | |
LayoutInflater li = LayoutInflater.from(parent.getContext()); | |
View v =li.inflate(R.layout.row, parent, false); | |
RecyclerCardViewHolder vh = new RecyclerCardViewHolder(v); | |
return vh; | |
} | |
@Override | |
public void onBindViewHolder(RecyclerCardViewHolder holder, int position) | |
{ | |
holder.company_name.setText(list.get(position).getCompany_name()); | |
holder.ae_name.setText(list.get(position).getAe_name()); | |
} | |
@Override | |
public int getItemCount() | |
{ | |
return list.size(); | |
} | |
} | |
} |
This file contains hidden or 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:id="@+id/not_found" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:gravity="center_horizontal" | |
android:orientation="vertical" | |
android:visibility="gone"> | |
<ImageView | |
android:layout_width="328px" | |
android:layout_height="400px" | |
android:background="?android:attr/panelBackground" | |
android:src="@drawable/not_found" /> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
android:text="@string/list_not_found" /> | |
</LinearLayout> |
This file contains hidden or 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"?> | |
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:id="@+id/card_view" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
app:cardCornerRadius="0dp" | |
android:layout_marginBottom="2sp" | |
android:layout_marginLeft="@dimen/activity_horizontal_margin" | |
android:layout_marginRight="@dimen/activity_horizontal_margin" | |
app:cardUseCompatPadding="true"> | |
<LinearLayout | |
android:id="@+id/card_layout" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_marginTop="@dimen/activity_vertical_margin" | |
android:layout_marginBottom="@dimen/activity_vertical_margin" | |
android:layout_marginLeft="@dimen/activity_horizontal_margin" | |
android:layout_marginRight="@dimen/activity_horizontal_margin" | |
android:orientation="vertical"> | |
<TextView | |
android:id="@+id/company_name" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="COM" /> | |
<TextView | |
android:id="@+id/ae_name" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:paddingTop="6sp" | |
android:text="name" | |
android:textSize="18sp" /> | |
</LinearLayout> | |
</android.support.v7.widget.CardView> |
This file contains hidden or 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"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content"> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/recycler_view" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_alignParentStart="true" | |
android:layout_marginTop="@dimen/activity_vertical_margin"/> | |
<include | |
layout="@layout/not_found" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerInParent="true" /> | |
</RelativeLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment