想为某些列表视图项添加“播放”图标

时间:2016-08-18 19:43:41

标签: java android listview imageview textview

**Check the image....This is where I want the Play icon**所以我想要的是在我的应用程序中,包含媒体播放器的项目很少,所以我想在右边对齐的列表视图项上放置一个“播放”图标。列表视图项包含图像视图和文本视图(都具有项目图标和项目名称)。 有什么办法可以将图片插入特定的项目

这是我的代码:

first.java:

public class first extends AppCompatActivity {
private ListView listView;
private String[] names = {"१. प्रार्थना",
        "२. सुखकर्ता दु:खहर्ता",
        "३. गजानना श्रीगणराया",
        "४. येई हो विठ्ठले",
        "५. लवथवती विक्राळा",
        "६. दुर्गे दुर्घट भारी",
        "७. आरती ज्ञानराजा",
        "८. हे भोळ्या शंकरा",
        "९. ओवाळू आरती",
        "१०. मंगेशा महारूद्रा",
        "११. आला गणराया",
        "१२. शेंदुर लाल चढायो",
        "१३. एकदंता",
        "१४. कर्पूर गौरा",
        "१५. हे गजवधना",
        "१६. प्रथमेश्वरा",
        "१७. जयदेवा हे  साईनाथा",
        "१८. श्री सत्यनारायणाची आरती",
        "१९. श्री महालक्ष्मीची आरती",
        "२०. ॐ जय लक्ष्मी माता",
        "२१. आरती संतोषी माता की",
        "२२. श्री हनुमंताची आरती",
        "२३. सुखंकर्ता की दु:खहर्ता",
        "२४. ॐ जय जगदीश",
        "२५. जय श्री विश्वकर्मा",
        "२६. हे गौरी गजानन",
        "२७. हे गजानना गौरीनंदना",
        "२८. आरती नवदुर्गेची",
        "२९. हे साईनाथा जगदिश्वरा",
        "३०. धन्य धन्य हो प्रदक्षिणा",
        "३१. घालीन लोटांगण"};
private int imageid[] = {R.drawable.prath,
        R.drawable.ganeshaji,
        R.drawable.ganpati,
        R.drawable.vita,
        R.drawable.shivji,
        R.drawable.durgamaa,
        R.drawable.nyan,
        R.drawable.bol,
        R.drawable.krishna,
        R.drawable.mangeshi,
        R.drawable.ala,
        R.drawable.shen,
        R.drawable.ekdant,
        R.drawable.karpur,
        R.drawable.gajavad,
        R.drawable.pratha,
        R.drawable.saibaba,
        R.drawable.satyanarayan,
        R.drawable.laxmimaa,
        R.drawable.omjailak,
        R.drawable.santoshimaa,
        R.drawable.hanuman,
        R.drawable.ganeshaji,
        R.drawable.skd,
        R.drawable.vishwakarma,
        R.drawable.ganpati,
        R.drawable.ganeshaji,
        R.drawable.navdurga,
        R.drawable.saibaba,
        R.drawable.danya,
        R.drawable.gana};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowHomeEnabled(true);
    actionBar.setIcon(R.mipmap.icon);
    final CustomList CustomList = new CustomList(this, names, imageid);
    listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(CustomList);
}
}

arti.java:

public class arti {
private String names;
private int imageid;
public arti(String names,int imageid){
    this.names=names;
    this.imageid=imageid;
}
public String getNames(){
    return this.names;
}
public int getImageid(){
    return this.imageid;
}
}

customlist.java:

public class CustomList extends ArrayAdapter<String> {
private String[] names;
private int[] imageid;
private Activity context;

public CustomList(Activity context, String[] names, int[] imageid) {
    super(context, R.layout.list_layout,names);
    this.context = context;
    this.names = names;
    this.imageid = imageid;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View listViewItem = inflater.inflate(R.layout.list_layout, null,    true);
    TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName);
    ImageView image = (ImageView) listViewItem.findViewById(R.id.imageView);
    textViewName.setText(names[position]);
    image.setImageResource(imageid[position]);
    return listViewItem;
    }

first.xml:

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listView"
    android:dividerHeight="2dp"
    android:layout_marginTop="3dp"
    android:layout_marginBottom="1dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

listlayout.xml:

<ImageView
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:id="@+id/imageView" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="80dp"
    android:gravity="center"
    android:textSize="22dp"
    android:text="New Text"
    android:id="@+id/textViewName"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/imageView"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

checkout this image

我希望将播放图像放在项目名称之后

4 个答案:

答案 0 :(得分:0)

只需检查getView()方法中的图片是否为-1,如下所示:

if (imageid[position] != -1) {
    image.setImageResource(imageid[position]);
} else {
    image.setVisibility(View.GONE); //or View.INVISIBLE
}

但是像这样你必须为你不想要图像的所有项目设置-1作为id

答案 1 :(得分:0)

是的,尝试使listlayout相对并使用如下选项:

“右侧”用于文本视图右侧的图像视图
显示或隐藏音乐图标的“可见性”。您必须在代码中启用可见性txtView.setVisibility(View.VISIBLE),因为默认情况下它是不可见的。

<RelativeLayout
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:id="@+id/headerLinearLay"
    android:orientation="horizontal">
   <TextView
       android:layout_width="wrap_content"
       android:layout_height="80dp"
       android:gravity="center"
       android:textSize="22dp"
       android:text="New Text"
       android:id="@+id/textViewName"
       android:layout_alignParentTop="true"
       android:layout_toRightOf="@+id/imageView"
       android:layout_alignParentRight="true"
       android:layout_alignParentEnd="true" />
   <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:id="@+id/imageView"
        android:layout_toRightOf="@id/textViewName"
        android:visibility="invisible" /> <!-- Enable when you want to show the play button -->
</RelativeLayout>

答案 2 :(得分:0)

在listlayout.xml中

<Button
android:layout_width="80dp"
android:layout_height="80dp"
android:visibility="gone"
android:id="@+id/playButtonView" />

<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/imageView" />

<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:gravity="center"
android:textSize="22dp"
android:text="New Text"
android:id="@+id/textViewName"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/imageView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

添加视图以保留播放按钮图像。可能有另一种方法可以让你更有意义,但这应该给你一个很好的起点。

在customlist.java中

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View listViewItem = inflater.inflate(R.layout.list_layout, null, true);
    TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName);
    ImageView image = (ImageView) listViewItem.findViewById(R.id.imageView);
    Button playButton = (ImageView) listViewItem.findViewById(R.id.playButtonView);
    if(playButtonShouldBeVisible) //change to fit your logic
        playButton.setVisibility(View.VISIBLE);
    textViewName.setText(names[position]);
    image.setImageResource(imageid[position]);
    return listViewItem;
}

编辑1:

解决以下评论。如果您的意思是您不需要播放图像的按钮(选项1),则只需更改

即可
<Button
android:layout_width="80dp"
android:layout_height="80dp"
android:visibility="gone"
android:id="@+id/playButtonView" />

<Button
android:layout_width="80dp"
android:layout_height="80dp"
android:visibility="gone"
android:id="@+id/playImageView" />

如果通过该评论意味着您的意思是您不需要额外的视图,那么您已经包含的图像视图就是您用于播放图像的视图。(选项2)我会更改< / p>

<Button
android:layout_width="80dp"
android:layout_height="80dp"
android:visibility="gone"
android:id="@+id/playButtonView" />

<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/imageView" />

<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:visibility="gone"
android:id="@+id/imageView" />

这些更改将在customlist.java中反映为

选项1:

ImageView image = (ImageView) listViewItem.findViewById(R.id.imageView);
Button playButton = (ImageView) listViewItem.findViewById(R.id.playButtonView);
if(playButtonShouldBeVisible) //change to fit your logic
    playButton.setVisibility(View.VISIBLE);

变为

ImageView image = (ImageView) listViewItem.findViewById(R.id.imageView);
ImageView playImage = (ImageView) listViewItem.findViewById(R.id.playImageView);
if(playImageShouldBeVisible) //change to fit your logic
    playImage.setVisibility(View.VISIBLE);

选项2:

ImageView image = (ImageView) listViewItem.findViewById(R.id.imageView);
Button playButton = (ImageView) listViewItem.findViewById(R.id.playButtonView);
if(playButtonShouldBeVisible) //change to fit your logic
    playButton.setVisibility(View.VISIBLE);

变为

ImageView image = (ImageView) listViewItem.findViewById(R.id.imageView);
if(imageViewShouldBeVisible) //change to fit your logic
    image.setVisibility(View.VISIBLE);

这些更改应该有助于指示在listlayout.xml中定义一个您可能希望显示的播放图像的视图,然后在Java代码中找到该视图并提供它的句柄,然后确定该视图是否应该是可见的并基于该更改可见性属性。

答案 3 :(得分:0)

只需执行以下步骤:

1。将带有图标的Button隐形按钮添加到xml:

   `<Button
    android:id="@+id/playButton"
    android:background="your_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:visibility="invisible" />`
适配器类中的

2。创建Set positions = new HashSet&lt;&gt;()并添加此集合所需的位置。

getView()方法中的

3。检查:

if(positions.contains(position)){ Button play = (Button) listViewItem.findViewById(R.id.playButton); play.setVisibility(View.VISIBLE);}

使用BaseAdapter而不是ArrayAdapter,只需扩展BaseAdapter并实现它的方法,在getCount()中返回列表项的总数,其他方法留空。 在getView()中,执行上面写的第3点 最终代码如下所示:

`public class Adapter extends ArrayAdapter {     private String []名称;     private int [] imageid;     private Activity mContext;     私人套装;

public Adapter(Context context, String[] names, int[] imageid) {
    super(context, R.layout.item);
    mContext = (Activity) context;
    this.names = names;
    this.imageid = imageid;
    fillSet();
}

private void fillSet() {
    set = new HashSet<>();
    set.add(1);
    set.add(2);
    set.add(6);
}

@Override
public int getCount() {
    return names.length;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    LayoutInflater inflater = mContext.getLayoutInflater();
    view = inflater.inflate(R.layout.item, null);
    TextView tv = (TextView) view.findViewById(R.id.tv);
    ImageView iv = (ImageView) view.findViewById(R.id.iv);
    tv.setText(names[i]);
    iv.setImageResource(imageid[i]);
    if (set.contains(i)) {
        Button button = (Button) view.findViewById(R.id.playButton);
        button.setVisibility(View.VISIBLE);
    }
    return view;
}

}`

它应该成为你想要的东西