【问题标题】:I want two listviews to scroll at the same time我希望两个列表视图同时滚动
【发布时间】:2016-07-25 15:31:07
【问题描述】:

我有一个活动,其中有两个列表视图。

一个是带有 imageview 和 textview insdie 的自定义列表视图,另一个是我找到滚动解决方案后的按钮。

基本上左边是食物清单及其图片,右侧是购买清单。
现在我无法同时滚动它们。

public class list_activity extends AppCompatActivity {

  String[] itemname ={
        "Safari",
        "Camera",
        "Global",
        "FireFox",
        "UC Browser",
        "Android Folder",
        "VLC Player",
        "Cold War",
        "Whatsapp",
        "Google",
        "Java"
  };
  String[] buy={"one","two","two","two","two","two","two","two","two","two","two","two","two","two","two","two","two"};
  Context context = this;


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_food_list);
    ListAdapter adapter = new com.example.azaabudeen.restoapp.CustomListView(this,itemname);
    ListView listView= (ListView)findViewById(R.id.listview);
    ListView secondlistview= (ListView)findViewById(R.id.foodlistBuyButton);
    ListAdapter secondA= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,buy);
    secondlistview.setAdapter(secondA);
    listView.setAdapter(adapter);

    final Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.view);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String currentItem= String.valueOf(parent.getItemAtPosition(position));
            Toast.makeText(list_activity.this,currentItem,Toast.LENGTH_SHORT).show();

         ///   final TextView text = (TextView)findViewById(R.id.text);
          ///  final Button myButton=(Button)findViewById(R.id.dialogButtonOK);
           /// text.setText("Image of the list");
           /// myButton.setOnClickListener(new View.OnClickListener() {
              ///  @Override
               /// public void onClick(View v) {
                 ////   dialog.dismiss();
               // }
           /// });
            dialog.show();
        }
    });
  }
}

更新:
我通过将 onClickListners 单独添加到 Customadapter 来做到这一点 这是代码

class CustomListView extends ArrayAdapter {
  public CustomListView(Context context, String[] resource) {
    super(context,R.layout.custom_view , resource);
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater MyInflater = LayoutInflater.from(getContext());
    View CustomView = MyInflater.inflate(R.layout.custom_view, parent, false);
    String SingleItem= (String) getItem(position);
    TextView text =(TextView)CustomView.findViewById(R.id.Itemname);
    ImageView Image= (ImageView)CustomView.findViewById(R.id.icon);

    text.setText(SingleItem);
    Image.setImageResource(R.drawable.myimage);
    final Button Buybutton= (Button)CustomView.findViewById(R.id.BuyButton);
    Buybutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getContext(), "Bought", Toast.LENGTH_SHORT).show();
        }
    });


    Image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getContext(),"Image",Toast.LENGTH_SHORT).show();

        }
    });
    return CustomView;
  }
} 

【问题讨论】:

  • 如果你想让它们同时滚动,将图像和按钮放在同一个列表视图上不是更容易吗?
  • 我希望在单击第一个列表视图时完成一些其他工作...例如,如果我单击图像..它应该被提示全屏,如果我单击按钮它应该做其他东西
  • @SarmadAijaz 您可以只使用一个列表视图来实现您想要的功能,因为您不需要两个列表视图
  • 在列表项的各个视图上设置点击侦听器,而不是使用 OnItemClickListener
  • 我应该在哪里添加 onClicklistners?你的意思是对所有三个单独的?

标签: java android listview


【解决方案1】:

您应该只使用一个ListView

您可以在自定义适配器中设置onClickListener

这里有一个例子:ListView with clickListener

为您的 imageView 和您的按钮设置 onClickListener

【讨论】:

    猜你喜欢
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    • 1970-01-01
    • 2019-01-06
    • 2013-01-21
    • 2019-12-10
    • 1970-01-01
    相关资源
    最近更新 更多