私のアプリは使用します
com.etsy.android.grid.StaggeredGridView
そして、ずらしたグリッドにカスタム項目を設定するアダプターがあります。そのグリッドに別のカスタム アイテムを追加したいと思います。これはできますか?
2 つのカスタム アイテム xml と 2 つのレイアウト アダプターがあります。staggeredgrid に両方を追加するにはどうすればよいですか? addVeiw() を使用してみましたが、staggeredGridview では使用できません。多分私は1つのアダプターだけを使うべきですか?
コードは最小限に抑えますが、もっと見たいかどうか尋ねてください。
import org.apache.http.NameValuePair;
public class GamesSummary_Fragment_Activity extends FragmentActivity
{
private ArrayList<String[]> loginTilesData;
private static final String TAG = "StaggeredGridActivityFragment";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
final FragmentManager fm = getSupportFragmentManager();
// Create the list fragment and add it as our sole content.
if (fm.findFragmentById(android.R.id.content) == null) {
final StaggeredGridFragment fragment = new StaggeredGridFragment();
fm.beginTransaction().add(android.R.id.content, fragment).commit();
}
}
private class StaggeredGridFragment extends Fragment implements AbsListView.OnScrollListener, AbsListView.OnItemClickListener
{
private StaggeredGridView stagggeredGridView;
private boolean mHasRequestedMore;
private TilesAdapter_Summary mAdapter;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_sgv, container, false);
}
@Override
public void onActivityCreated(final Bundle savedInstanceState) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
//Encapsulate all within a post create from a async task or call a blocking http call
super.onActivityCreated(savedInstanceState);
stagggeredGridView = (StaggeredGridView) getView().findViewById(R.id.grid_view);
if (mAdapter == null) {
mAdapter = new TilesAdapter_Summary(getActivity(), R.id.summary1_value);
}
for (String[] data : loginTilesData) {
mAdapter.add(data); //Add each loginTilesData TileAdapter element to an mAdapter where it will be further broken down and used by the TileAdapter
}
//Add controls item layout
final LayoutInflater layoutInflator = getActivity().getLayoutInflater();
View viewControls = layoutInflator.inflate(R.layout.grid_item_controls, null);
staggeredGridView.addView(viewControls, 1); //THIS FAILS WITH ERROR SAYING PARENT CLASS CANT DO 'addVeiw()
stagggeredGridView.setAdapter(mAdapter);
stagggeredGridView.setOnScrollListener(this);
stagggeredGridView.setOnItemClickListener(this);
}
}
public class TilesAdapter_Summary extends ArrayAdapter<String[]> {
private static final String TAG = "TilesAdapter";
static class ViewHolder {
ImageView summary_image;
TextView summary1_label;
TextView summary2_label;
}
adapter_summary クラス
private final LayoutInflater mLayoutInflater;
private final Random mRandom;
private final ArrayList<Integer> mBackgroundColors;
private static final SparseArray<Double> sPositionHeightRatios = new SparseArray<Double>();
public TilesAdapter_Summary(final Context context, final int textViewResourceId) {
super(context, textViewResourceId);
mLayoutInflater = LayoutInflater.from(context);
mRandom = new Random();
mBackgroundColors = new ArrayList<Integer>();
mBackgroundColors.add(R.color.green);
mBackgroundColors.add(R.color.blue);
mBackgroundColors.add(R.color.yellow);
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent)
{
//Init the Viewholder with the controls we want to populate
ViewHolder vh;
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.grid_item_summary, parent, false);
vh = new ViewHolder();
vh.summary_image = (ImageView) convertView.findViewById(R.id.summary_image);
vh.summary1_label = (TextView) convertView.findViewById(R.id.summary1_label);
vh.summary2_label = (TextView) convertView.findViewById(R.id.summary2_label);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
double positionHeight = getPositionRatio(position);
int backgroundIndex = position >= mBackgroundColors.size() ?
position % mBackgroundColors.size() : position;
convertView.setBackgroundResource(mBackgroundColors.get(backgroundIndex));
Log.d(TAG, "getView position:" + position + " h:" + positionHeight);
String[] tileControlValues = getItem(position); //Get this TileAdapters item and split it up
//Assign the tileControlValues to controls
vh.summary_header.setText(tileControlValues[0]);
vh.summary_subheader.setText(tileControlValues[1]);
vh.summary1_label.setText(tileControlValues[2]);
vh.summary1_value.setText(tileControlValues[3]);
vh.summary2_label.setText(tileControlValues[4]);
vh.summary2_value.setText(tileControlValues[5]);
return convertView;
}
private double getPositionRatio(final int position) {
double ratio = sPositionHeightRatios.get(position, 0.0);
// if not yet done generate and stash the columns height
// in our real world scenario this will be determined by
// some match based on the known height and width of the image
// and maybe a helpful way to get the column height!
if (ratio == 0) {
ratio = getRandomHeightRatio();
sPositionHeightRatios.append(position, ratio);
Log.d(TAG, "getPositionRatio:" + position + " ratio:" + ratio);
}
return ratio;
}
private double getRandomHeightRatio() {
return (mRandom.nextDouble() / 2.0) + 1.0; // height will be 1.0 - 1.5 the width
}
}