我正在为布局添加动态按钮。每个按钮一次添加一个,如果根据用户的操作添加了新按钮,则布局必须自行更新。所以布局可以有3个按钮,或16个,或者其他什么,具体取决于用户的动作。并且可以在不同时间添加按钮。因此,如果用户打开应用程序并添加按钮,则离开并返回到应用程序并添加另一个按钮,旧按钮必须保留。
我希望将我的按钮逐个添加到这样的布局中:
我已经了解了如何执行此操作,并且建议我使用带有GridLayoutManager的RecyclerView。我已将此添加到我的代码中,但问题是,当我添加一个按钮,然后如果我添加另一个按钮,则第二个添加在第一个按钮之上。因此,如果用户操作说应该制作16个按钮,我只是将16个按钮放在彼此的顶部而不是我想要的布局。
这是我的代码:
启动RecyclerView的主要片段:我有另一个活动启动" createButton"方法并传递drawable和字符串。这些drawable和字符串将根据用户的操作一次一个地传递给此方法,并一次创建一个图像按钮
public class MyFragment extends Fragment {
private GridLayoutManager lLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
// onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment, container, false);
// Create an empty list to initialize the adapter (or else get nullPointerException error)
List<ItemObject> myList = new ArrayList<ItemObject>();
lLayout = new GridLayoutManager(getActivity(), 4, GridLayoutManager.HORIZONTAL, false);
RecyclerView rView = (RecyclerView)view.findViewById(R.id.recycler_view);
rView.setHasFixedSize(true);
rView.setLayoutManager(lLayout);
RecyclerViewAdapter rcAdapter = new RecyclerViewAdapter(getActivity(),myList);
rView.setAdapter(rcAdapter);
return view;
}
private List<ItemObject> getAllItemList(String applicationName, Drawable app_drawable){
List<ItemObject> allItems = new ArrayList<ItemObject>();
allItems.add(new ItemObject(applicationName, app_drawable));
return allItems;
}
public void createButton (Drawable d, String appName){
List<ItemObject> rowListItem = getAllItemList(appName, d);
lLayout = new GridLayoutManager(getActivity(), 2, GridLayoutManager.HORIZONTAL, false);
RecyclerView rView = (RecyclerView)getView().findViewById(R.id.recycler_view);
rView.setHasFixedSize(true);
rView.setLayoutManager(lLayout);
RecyclerViewAdapter rcAdapter = new RecyclerViewAdapter(getActivity(), rowListItem);
rView.setAdapter(rcAdapter);
}
}
这是RecyclerViewHolders:
public class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView AppName;
public ImageButton AppButton;
public RecyclerViewHolders(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
AppName = (TextView)itemView.findViewById(R.id.new_app_name);
AppButton = (ImageButton)itemView.findViewById(R.id.new_app_button);
}
@Override
public void onClick(View v) {
}
}
RecyclerViewAdapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {
private List<ItemObject> itemList;
private Context context;
public RecyclerViewAdapter(Context context, List<ItemObject> itemList) {
this.itemList = itemList;
this.context = context;
}
@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_fragment, null);
RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
return rcv;
}
@Override
public void onBindViewHolder(RecyclerViewHolders holder, int position) {
holder.AppName.setText(itemList.get(position).getName());
holder.AppButton.setImageDrawable(itemList.get(position).getPhoto());
}
@Override
public int getItemCount() {
return this.itemList.size();
}
}
ItemObject
public class ItemObject {
private String name;
private Drawable d;
public ItemObject(String name, Drawable d) {
this.name = name;
this.d = d;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Drawable getPhoto() {
return d;
}
public void setPhoto(Drawable d) {
this.d = d;
}
}
和我的布局(my_fragment)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:layout_marginTop="15dp"
android:id="@+id/my_fragment"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="horizontal" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/new_app_button"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/new_app_name"
android:gravity="center"
android:layout_below="@+id/new_app_button"
/>
</RelativeLayout>
</LinearLayout>
解答:
这是我如何在手机和平板电脑上进行不同的布局(手机上有2排,平板电脑上有3排)
此代码已添加到MyFragment的onCreateView方法
// Get screen size so we can have different layouts for phone and tablet
int screenSize = getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK;
String toastMsg;
switch(screenSize) {
case Configuration.SCREENLAYOUT_SIZE_LARGE:
toastMsg = "Large screen";
Log.d("tag_name", "Large screen");
break;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
toastMsg = "Normal screen";
Log.d("tag_name", "Normal screen");
break;
case Configuration.SCREENLAYOUT_SIZE_SMALL:
toastMsg = "Small screen";
Log.d("tag_name", "Small screen");
break;
default:
toastMsg = "Screen size is neither large, normal or small";
Log.d("tag_name", "Screen size is not large, normal, or small");
}
Toast.makeText(getActivity(), toastMsg, Toast.LENGTH_LONG).show();
// Create an empty list to initialize the adapter (or else get nullPointerException error)
List<ItemObject> myList = new ArrayList<ItemObject>();
if (screenSize == Configuration.SCREENLAYOUT_SIZE_LARGE
|| screenSize == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
lLayout = new GridLayoutManager(getActivity(), 3, GridLayoutManager.HORIZONTAL, false);
}
else lLayout = new GridLayoutManager(getActivity(), 2, GridLayoutManager.HORIZONTAL, false);
答案 0 :(得分:1)
您可以使用此answer检测屏幕尺寸。
如果屏幕尺寸为Configuration.SCREENLAYOUT_SIZE_LARGE
或Configuration.SCREENLAYOUT_SIZE_XLARGE
,那么它可能是平板电脑。
int spanCount = 2;
if (screenSize == Configuration.SCREENLAYOUT_SIZE_LARGE
or screenSize == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
spanCount = 3;
}