将水平滚动视图添加到 ListView

Posted

技术标签:

【中文标题】将水平滚动视图添加到 ListView【英文标题】:Adding a Horizontal Scroll View to a ListView 【发布时间】:2016-11-24 07:10:51 【问题描述】:

我目前有一个适配器,它需要两个图像和一个文本视图来添加到listView。我想更改它,以便可以在每个包含图像的列表项中添加一个horizontalScrollView。有没有办法做到这一点?

这是我当前工作的适配器,如上所述,带有两个 imageView 和一个 textView

    public class MyAdapter extends BaseAdapter 
    private Context mContext;
    private List<Bean> mList;
    private PopupWindow popUpWindow;
    private LayoutInflater inflater;

    public MyAdapter(Context context,List<Bean> list)
        mContext=context;
        mList=list;


    

    @Override
    public int getCount() 
        return mList.size();
    

    @Override
    public Object getItem(int position) 
        return mList.get(position);
    

    @Override
    public long getItemId(int position) 
        return position;
    

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
        ViewHolder holder;
        //use convertView recycle
        if(convertView==null)
            holder=new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.content_orders, parent, false);
            holder.textView= (TextView) convertView.findViewById(R.id.textView2);
            holder.imageView= (ImageView) convertView.findViewById(R.id.imageView2);
            holder.imageView2 = (ImageView) convertView.findViewById(R.id.imageView3);
            holder.information= (Button) convertView.findViewById(R.id.button5);
            convertView.setTag(holder);
        else
            holder = (ViewHolder) convertView.getTag();
        

        //set text and url
        final View finalConvertView = convertView;
        holder.information.setOnClickListener(new View.OnClickListener() 



            @Override
            public void onClick(View v) 
                inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                ViewGroup container = (ViewGroup) inflater.inflate(R.layout.information_popup, null);
                popUpWindow = new PopupWindow(container, 800,400,true);
                popUpWindow.showAtLocation(finalConvertView.findViewById(R.id.orders), Gravity.CENTER, 0,0);

                container.setOnTouchListener(new View.OnTouchListener() 
                    @Override
                    public boolean onTouch(View view, MotionEvent motionEvent) 
                        popUpWindow.dismiss();
                        return true;
                    
                );
            
        );

        holder.textView.setText(mList.get(position).getText());
        Picasso.with(mContext).load(mList.get(position).getUrl()).resize(450,450).into(holder.imageView);
        Picasso.with(mContext).load(mList.get(position).getUrl2()).resize(450,450).into(holder.imageView2);

        return convertView;
    

    class ViewHolder
        TextView textView;
        ImageView imageView;
        ImageView imageView2;
        Button information;
    
    

Getter 和 Setter 类

    public class Bean 
    String text;
    String url;
    String url2;



    public String getText() 
        return text;
    

    public void setText(String text) 
        this.text = text;
    

    public String getUrl() 
        return url;
    

    public void setUrl(String url) 
        this.url = url;
    
    public String getUrl2() 
        return url2;
    

    public void setUrl2(String url2) 
        this.url2 = url2;
    
    

活动布局 XML

    <android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinator"
    android:layout_
    android:layout_
    android:fitsSystemWindows="true"
    tools:context=".OrdersActivity"
    android:background="#ffffff">

    <android.support.design.widget.AppBarLayout
        android:layout_
        android:layout_
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_
            android:layout_
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>

<ListView
    android:layout_
    android:layout_
    android:id="@+id/orderListView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:dividerHeight="2dp"/>

</android.support.design.widget.CoordinatorLayout>

内容布局 XML

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".OrdersActivity"
    android:id="@+id/orders">


    <TextView
        android:id="@+id/textView2"
        android:layout_
        android:layout_
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/imageView2"
        android:src="@drawable/one"
        android:layout_
        android:layout_
        android:layout_below="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageView
        android:layout_
        android:layout_
        android:src="@drawable/one"
        android:id="@+id/imageView3"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignBottom="@+id/imageView2"
        android:layout_alignTop="@+id/imageView2"/>

    <Button
        android:layout_
        android:layout_
        android:text="@string/informaci_n"
        android:id="@+id/button5"
        android:layout_below="@+id/button4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_
        android:layout_
        android:text="@string/entregado"
        android:id="@+id/button3"
        android:layout_below="@+id/imageView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_
        android:layout_
        android:text="@string/no_entregado"
        android:id="@+id/button4"
        android:layout_below="@+id/button3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />


</RelativeLayout>    

【问题讨论】:

检查 twowaygridview 并将行数限制为 1 【参考方案1】:

我最终弄明白了。如果其他人想要这样做,这是我使用的代码。我刚刚添加了一个horizontalScrollView,我在horizontalScrollView 中添加了一个水平方向的linearLayout,然后我在linearLayout 中添加了图像。这是我的代码。

主活动

public class MainActivity extends AppCompatActivity 
ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    String a1 = "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Soccer_ball.svg/2000px-Soccer_ball.svg.png";
    String a2 = "https://s-media-cache-ak0.pinimg.com/736x/91/27/24/912724cb45a6a2ba468024a5285b01e7.jpg";

    String[] descriptionArray = "Description 1", "Description 2";
    String[] photoArray = a1,a2,a1,a2,a1,a2,a1;
    String[] photoArray2 = a2,a1,a2,a1,a2,a1,a2;
    String[] photoArray3 = a1,a2,a1,a2,a1,a2,a1;
    String [] photoArray4 = a2,a1,a2,a1,a2,a1,a2;
    String[] photoArray5 = a1,a2,a1,a2,a1,a2,a1;
    String[] photoArray6 = a2,a1,a2,a1,a2,a1,a2;
    String[] photoArray7 = a1,a2,a1,a2,a1,a2,a1;





    list = (ListView) findViewById(R.id.listView);
    List<Bean> myList = new ArrayList<>();

    for(int i = 0; i < descriptionArray.length; i++) 
        Bean bean = new Bean();
        bean.setText(descriptionArray[i]);
        bean.setUrl(photoArray[i]);
        bean.setUrl2(photoArray2[i]);
        bean.setUrl3(photoArray3[i]);
        bean.setUrl4(photoArray4[i]);
        bean.setUrl5(photoArray5[i]);
        bean.setUrl6(photoArray6[i]);
        bean.setUrl7(photoArray7[i]);
        myList.add(bean);
    

    MyAdapter adapter = new MyAdapter(MainActivity.this, myList);
    list.setAdapter(adapter);



我的适配器

 public class MyAdapter extends BaseAdapter 
    private Context mContext;
    private List<Bean> mList;


    public MyAdapter(Context context,List<Bean> list)
        mContext=context;
        mList=list;


    

    @Override
    public int getCount() 
        return mList.size();
    

    @Override
    public Object getItem(int position) 
        return mList.get(position);
    

    @Override
    public long getItemId(int position) 
        return position;
    

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
        ViewHolder holder;
        //use convertView recycle
        if(convertView==null)
            holder=new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.content_main, parent, false);
            holder.textView= (TextView) convertView.findViewById(R.id.textView);
            holder.imageView= (ImageView) convertView.findViewById(R.id.imageView);
            holder.imageView2 = (ImageView) convertView.findViewById(R.id.imageView2);
            holder.imageView3 = (ImageView) convertView.findViewById(R.id.imageView3);
            holder.imageView4 = (ImageView) convertView.findViewById(R.id.imageView4);
            holder.imageView5 = (ImageView) convertView.findViewById(R.id.imageView5);
            holder.imageView6 = (ImageView) convertView.findViewById(R.id.imageView6);
            holder.imageView7 = (ImageView) convertView.findViewById(R.id.imageView7);
            holder.horizontalScrollView= (HorizontalScrollView) convertView.findViewById(R.id.horizontalScrollView);
            convertView.setTag(holder);
        else
            holder = (ViewHolder) convertView.getTag();
        




        holder.textView.setText(mList.get(position).getText());
        Picasso.with(mContext).load(mList.get(position).getUrl()).resize(200,200).into(holder.imageView);
        Picasso.with(mContext).load(mList.get(position).getUrl2()).resize(200,200).into(holder.imageView2);
        Picasso.with(mContext).load(mList.get(position).getUrl3()).resize(200,200).into(holder.imageView3);
        Picasso.with(mContext).load(mList.get(position).getUrl4()).resize(200,200).into(holder.imageView4);
        Picasso.with(mContext).load(mList.get(position).getUrl5()).resize(200,200).into(holder.imageView5);
        Picasso.with(mContext).load(mList.get(position).getUrl6()).resize(200,200).into(holder.imageView6);
        Picasso.with(mContext).load(mList.get(position).getUrl7()).resize(200,200).into(holder.imageView7);



        return convertView;
    

    class ViewHolder
        TextView textView;
        ImageView imageView;
        ImageView imageView2;
        ImageView imageView3;
        ImageView imageView4;
        ImageView imageView5;
        ImageView imageView6;
        ImageView imageView7;
        HorizontalScrollView horizontalScrollView;
    

Bean 类(Getter 和 Setter 类)

public class Bean 
String text;
String url;
String url2;
String url3;
String url4;
String url5;
String url6;
String url7;
public String getText() 
    return text;


public void setText(String text) 
    this.text = text;


public String getUrl() 
    return url;


public void setUrl(String url) 
    this.url = url;


public String getUrl2() 
    return url2;


public void setUrl2(String url2) 
    this.url2 = url2;


public String getUrl3() 
    return url3;


public void setUrl3(String url3) 
    this.url3 = url3;


public String getUrl4() 
    return url4;


public void setUrl4(String url4) 
    this.url4 = url4;


public String getUrl5() 
    return url5;


public void setUrl5(String url5) 
    this.url5 = url5;


public String getUrl6() 
    return url6;


public void setUrl6(String url6) 
    this.url6 = url6;


public String getUrl7() 
    return url7;


public void setUrl7(String url7) 
    this.url7 = url7;


activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:fitsSystemWindows="true"
    tools:context=".MainActivity"
    android:background="#ffffff">

    <android.support.design.widget.AppBarLayout
        android:layout_
        android:layout_
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_
            android:layout_
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/list_container" />

</android.support.design.widget.CoordinatorLayout>

content_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity">


    <TextView
        android:layout_
        android:layout_
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />


    <HorizontalScrollView
        android:layout_
        android:layout_
        android:id="@+id/horizontalScrollView"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true" >
    <LinearLayout
        android:id="@+id/xml_full_img_linear_below_view"
        android:layout_
        android:layout_
        android:layout_gravity="center"
        android:orientation="horizontal" >

        <ImageView
            android:layout_
            android:layout_
            android:id="@+id/imageView" />
        <ImageView
            android:layout_
            android:layout_
            android:id="@+id/imageView2" />
        <ImageView
            android:layout_
            android:layout_
            android:id="@+id/imageView3" />
        <ImageView
            android:layout_
            android:layout_
            android:id="@+id/imageView4" />
        <ImageView
            android:layout_
            android:layout_
            android:id="@+id/imageView5" />
        <ImageView
            android:layout_
            android:layout_
            android:id="@+id/imageView6" />
        <ImageView
            android:layout_
            android:layout_
            android:id="@+id/imageView7" />
    </LinearLayout>
    </HorizontalScrollView>
</RelativeLayout>

list_container.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_
    android:layout_
    android:paddingTop="53dp"
    tools:context=".MainActivity">

    <ListView
        android:layout_
        android:layout_
        android:id="@+id/listView"
        android:dividerHeight="2dp"/>


</RelativeLayout>

【讨论】:

【参考方案2】:

不完全是你问的,但我认为这个问题可以帮助你。

HorizontalScrollView inside ListView: minor vertical scroll stops horizontal scroll

【讨论】:

以上是关于将水平滚动视图添加到 ListView的主要内容,如果未能解决你的问题,请参考以下文章

将文本字段动态添加到表格视图单元格并水平滚动

滚动视图不水平滚动

如何滚动 ListView 中的项目使其可见?

如何滚动到添加到 MVVM Light 列表视图中的新项目

如何水平居中滚动视图的内容?

如何将滚动视图嵌入到表格视图中