Android 能否将两种类型的项目添加到stggeredListView?
我的应用程序使用 com.etsy.android.grid.edgridview 我有一个适配器,它用一个自定义项填充交错网格。现在我想向该网格添加另一个自定义项。这能做到吗 我有2个自定义项XML和2个LayoutAdapter。如何将这两个项都添加到Edgrid中?我尝试使用AddVew,但它在视图中不可用。也许我应该只使用1个适配器 我会把代码控制在最低限度,但如果你想看更多的话,我会问你Android 能否将两种类型的项目添加到stggeredListView?,android,staggered-gridview,Android,Staggered Gridview,我的应用程序使用 com.etsy.android.grid.edgridview 我有一个适配器,它用一个自定义项填充交错网格。现在我想向该网格添加另一个自定义项。这能做到吗 我有2个自定义项XML和2个LayoutAdapter。如何将这两个项都添加到Edgrid中?我尝试使用AddVew,但它在视图中不可用。也许我应该只使用1个适配器 我会把代码控制在最低限度,但如果你想看更多的话,我会问你 import org.apache.http.NameValuePair; public cla
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;
}
适配器\u摘要类
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
}
}
基于您正在尝试解决的问题的类型,一个很好的替代选择是考虑利用Debug GrIDLayOutMaultReaveReVIEW,并定制适配器以2个或更多个(如果需要的类型的浏览器)工作,允许它们中的每一个拥有它自己的布局。 更新:
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
if(getItemViewType(position) == 1) {
// work with ViewHolderTypeOne
} else if(getItemViewType(position) == 2) {
// work with ViewHolderTypeTwo
}
}
@Override
public int getItemViewType(int i) {
// View Types: 1 - view type one, 2 - view type two, determined by position i in grid
if(i == 0) {
return 1;
} else {
return 2;
}
}
class ViewHolderTypeOne extends ViewHolder {
public viewOneAttrOne;
public viewOneAttrTwo;
}
class ViewHolderTypeTwo extends ViewHolder {
public viewTwoAttrOne;
public viewTwoAttrTwo;
public viewTwoAttrThree;
}
我得到了一个答案,不一定是最好的答案,所以我会让它打开一点 我将新的项目xml控件合并到旧的项目xml控件中,因此只有一个,然后我确定在将项目添加到网格时要显示哪些控件。给人的印象是一个网格中有两个不同的项目…在将项目添加到页面时…我向字符串数组添加了一个新元素,该数组将触发要构建的新项目xml-我认识到这个新元素希望显示不同的控件,因此我将原始控件视为不可见,将新控件视为可见
if (tileControlValues[0].equals("controls"))
{
vh.layout_summary.setVisibility(View.INVISIBLE);
vh.layout_controls.setVisibility(View.VISIBLE);
}
else
{
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]);
哦,亲爱的,我的方式太深了,回收器视图不是一个简单的通道。例如:有没有使用交错视图我可以做到这一点-我目前正在研究一个适配器,它在等待切换时引用一个项目xml或其他。你能不能也显示TileAdapter_摘要类的内容?当然。。。接下来……如果您想要我的sumary布局xml和新的布局xml,请告诉我;'controls’我现在考虑在适配器中使用一个开关,并根据活动传递的开关指向diff xms,但这并没有帮助,因为我只初始化了适配器一次,所以永远不会添加第二项。我想在添加了摘要图块之后,将新自定义视图的一个实例添加到网格中。我想我可以合并视图;显示我想要的内容并隐藏其他内容以提供2个视图的外观,但这不是很令人满意。更新此内容,将可见性设置为View.Gone,以完全释放用于您想要不可见的控件的空间。