gpt4 book ai didi

类似于 Google play 的 Android ListView 布局

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:51:51 25 4
gpt4 key购买 nike

我想实现一个类似于 Google Play 的列表布局,其中每个行都有菜单。请帮我创建这个。

Google Play List view

我是否需要创建一个弹出式菜单,或者有任何可用的选项来实现这一点。

谢谢

最佳答案

看起来您正在尝试完全按照图中显示的方式进行操作。我只是举例说明我是如何尝试实现这一目标的。

这是我的做法。不是很困难。直接执行弹出菜单。

第 1 步:我的适配器

 public class ListAdapter extends BaseAdapter{

private ArrayList<String> mainList;


public ListAdapter(Context applicationContext,
ArrayList<String> questionForSliderMenu) {

super();

this.mainList = questionForSliderMenu;

}

public ListAdapter() {

super();
this.mainList = QuestionForSliderMenu;

}

@Override
public int getCount() {

return mainList.size();
}

@Override
public Object getItem(int position) {

return mainList.get(position);
}

@Override
public long getItemId(int position) {

return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

if (convertView == null) {

LayoutInflater inflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.custom_row_stack, null);
}


TextView tv1 = (TextView) convertView
.findViewById(R.id.row_textView1);
TextView tv2 = (TextView) convertView
.findViewById(R.id.row_install_textView1);
ImageView imageIcon = (ImageView) convertView
.findViewById(R.id.row_imageView1);
ImageView imageClick = (ImageView) convertView
.findViewById(R.id.row_click_imageView1);

try {

tv1.setText(" List Item "+ " : " + position);
imageClick.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {



switch (v.getId()) {
case R.id.row_click_imageView1:

PopupMenu popup = new PopupMenu(getApplicationContext(), v);
popup.getMenuInflater().inflate(R.menu.clipboard_popup,
popup.getMenu());
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {

switch (item.getItemId()) {
case R.id.install:

//Or Some other code you want to put here.. This is just an example.
Toast.makeText(getApplicationContext(), " Install Clicked at position " + " : " + position, Toast.LENGTH_LONG).show();

break;
case R.id.addtowishlist:

Toast.makeText(getApplicationContext(), "Add to Wish List Clicked at position " + " : " + position, Toast.LENGTH_LONG).show();

break;

default:
break;
}

return true;
}
});

break;

default:
break;
}



}
});

} catch (Exception e) {

e.printStackTrace();
}

return convertView;
}

}

第 2 步:在 Activity 中,只需设置适配器:

public class CustomListActivity extends Activity {

String[] numbers = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King" };
ArrayList<String> QuestionForSliderMenu = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.listview_layout);

ListView listView = (ListView) findViewById(R.id.customlistView1);




for (String s : numbers) {

QuestionForSliderMenu.add(s);

}

ListAdapter mAdapter = new ListAdapter(this, QuestionForSliderMenu);

listView.setAdapter(mAdapter);

}

第 3 步:自定义行项目/布局:

custom_row_stack.xml

<?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"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<ImageView
android:id="@+id/row_imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:src="@drawable/page1" />

<TextView
android:id="@+id/row_textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:text="Some Item"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#333333" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ImageView
android:id="@+id/row_click_imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="right"
android:clickable="true"
android:src="@drawable/dots" />

<TextView
android:id="@+id/row_install_textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:padding="10dp"
android:text="Install"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#333333" />
</LinearLayout>
</LinearLayout>

第四步:我的弹出菜单.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/install"
android:title="Install" />
<item
android:id="@+id/addtowishlist"
android:title="Add to wishlist" />
</menu>

最后:它的外观截图。

ListView http://imageshack.com/a/img822/4144/umdy.png ListView http://imageshack.com/a/img32/9839/ne90.png ListView http://imageshack.com/a/img198/7404/prqc.png

如果有更好的解决方案,对我也很有帮助。希望这有帮助..:)

关于类似于 Google play 的 Android ListView 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22294691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com