如何在列表视图项中添加imagebutton?

时间:2018-06-24 15:40:21

标签: android listview android-imagebutton

我有一个列表视图,该视图动态加载了mysql数据库中的视频链接。当我单击列表视图的元素时,我启动了与该元素相对应的视频,但是现在我想在列表视图的每个元素中添加一个imagabutton,以便用户可以下载相应的视频。我知道如何使用下载管理器来管理下载,但是如何在不取消lisview元素单击的情况下使我的图像按钮可点击,以及如何下载与listview的每个元素相对应的确切视频?

这是我的适配器

public class CustomAdapter extends ArrayAdapter<String> {

private String[] androidosnames;
private String[] titre;
private String[] url;
private Bitmap[] bitmaps;
private Activity context;

public CustomAdapter(Activity context, String[] androidosnames, String[] titre, Bitmap[] bitmaps, String[] url) {
    super(context, R.layout.list_item, androidosnames);
    this.context = context;
    this.url = url;
    this.titre = titre;
    this.bitmaps = bitmaps;
    this.androidosnames = androidosnames;

}

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

    LayoutInflater inflater = context.getLayoutInflater();
    final View listViewItem = inflater.inflate(R.layout.list_item, null, true);

    TextView androidos = listViewItem.findViewById(R.id.tvandroidosnames);
    TextView textView = listViewItem.findViewById(R.id.url);
    TextView titresong = listViewItem.findViewById(R.id.titre);

    androidos.setText(androidosnames[position]);
    titresong.setText(titre[position]);
    textView.setText(url[position]);

    textView.setVisibility(View.INVISIBLE);
    textView.setSelected(true);
    androidos.setSelected(true);

    ImageView image = listViewItem.findViewById(R.id.imgvw);
    image.setImageBitmap(Bitmap.createScaledBitmap(bitmaps[position], 100, 100, false));

    return listViewItem;

这是我的列表视图项

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
    android:id="@+id/imgvw"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"/>

<TextView
    android:id="@+id/titre"
    android:layout_width="150dp"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="14dp"
    android:layout_marginStart="14dp"
    android:layout_toEndOf="@+id/imgvw"
    android:layout_toRightOf="@+id/imgvw"
    android:text="New Text"
    android:textSize="20sp"
    android:textStyle="bold"
    android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>

<TextView
    android:id="@+id/tvandroidosnames"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toEndOf="@+id/imgvw"
    android:layout_toRightOf="@+id/imgvw"
    android:layout_below="@+id/titre"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:textColor="@color/colorAccent"
    android:text="url;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"
    android:layout_marginLeft="14dp"
    android:typeface="sans"
    android:layout_marginStart="14dp" />
<TextView
    android:id="@+id/url"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toEndOf="@+id/imgvw"
    android:layout_toRightOf="@+id/imgvw"
    android:layout_below="@+id/titre"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:textColor="@color/colorAccent"
    android:text="url;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"
    android:layout_marginLeft="14dp"
    android:typeface="sans"
    android:layout_marginStart="14dp" />
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_centerVertical="true"
    app:srcCompat="@drawable/ic_file_download_black_24dp"
    android:layout_marginEnd="5dp"
    android:layout_marginRight="5dp"/>
<ImageView
    android:id="@+id/coeur"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_centerVertical="true"
    app:srcCompat="@drawable/ic_like"
    android:layout_marginEnd="50dp"
    android:layout_marginRight="50dp"/>
<TextView
    android:id="@+id/counter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginEnd="85dp"
    android:layout_marginRight="85dp"/>

,这是允许我在其他活动中开始播放视频的代码

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent intent = new Intent(MainActivity.this, PlayVideo.class);
            intent.putExtra("url", GetJson.urla[i]);
            intent.putExtra("name", GetJson.Android_Name[i]);
            intent.putExtra("title", GetJson.titre[i]);
            startActivity(intent);
            overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
            finish();
        }
    });

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

尝试以下操作:(这里的想法是为列表内每个项目的每个视图添加onClickListener()-当用户单击除imageView以外的任何其他内容时,启动视频,否则下载视频。未经测试,此代码。如果在测试时遇到问题,请在下面写下评论)。

1)MnnnnnnnActivity.class:-------------

 public class MnnnnnnnActivity extends AppCompatActivity implements ListItemListener {

private ListView lv;
private CustomAdapter customAdapter;
private String[] androidnames;
private String[] titre;
private Bitmap[] bitmaps;
private String[] url;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.layout6);

    //populate arrays (androidnames, titre , bitmaps , url);

    lv = (ListView) findViewById(R.id.lv);
    customAdapter = new CustomAdapter(getApplicationContext() , androidnames , titre , bitmaps , url , MnnnnnnnActivity.this);
    lv.setAdapter(customAdapter);

}

@Override
public void Launch_Video(int position) {

}

@Override
public void Download_Video(int position) {

}
}

2)CustomAdapter.class:-------

public class CustomAdapter extends ArrayAdapter<String> {

private String[] androidosnames;
private String[] titre;
private String[] url;
private Bitmap[] bitmaps;
private Context context;
private ListItemListener callback;

public CustomAdapter(Context context, String[] androidosnames, String[] titre, Bitmap[] bitmaps, String[] url , ListItemListener l) {
    super(context, R.layout.list_view_item, androidosnames);
    this.context = context;
    this.url = url;
    this.titre = titre;
    this.bitmaps = bitmaps;
    this.androidosnames = androidosnames;
    if(l != null){
        callback = l;
    }

}

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

    ViewHolder holder;

    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(R.layout.list_view_item, null);
        holder = new ViewHolder();

        holder.androidos = (TextView) convertView.findViewById(R.id.tvandroidosnames);
        holder.androidos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(callback != null){
                    callback.Launch_Video(position);
                }
            }
        });


        holder.textView = (TextView)convertView.findViewById(R.id.url);
        holder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(callback != null){
                    callback.Launch_Video(position);
                }
            }
        });


        holder.titresong = (TextView) convertView.findViewById(R.id.titre);
        holder.titresong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(callback != null){
                    callback.Launch_Video(position);
                }
            }
        });


        holder.image = (ImageView) convertView.findViewById(R.id.imgvw);
        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(callback != null){
                    callback.Download_Video(position);
                }
            }
        });

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.androidos.setText(androidosnames[position]);
    holder.titresong.setText(titre[position]);
    holder.textView.setText(url[position]);

    holder.textView.setVisibility(View.INVISIBLE);
    holder.textView.setSelected(true);
    holder.androidos.setSelected(true);

    holder.image.setImageBitmap(Bitmap.createScaledBitmap(bitmaps[position], 100, 100, false));

    return convertView;

}

private class ViewHolder {
    TextView androidos;
    TextView textView;
    TextView titresong;
    ImageView image;
}

}

3)ListItemListener接口:----------

public interface ListItemListener {

public void Launch_Video(int position);

public void Download_Video(int position);
}

4)layout6.xml:--------

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lv">
</ListView>

</android.support.constraint.ConstraintLayout>

5)list_view_item.xml与list_item相同。