2016-08-18 58 views
0

**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

我想表演圖像放置的物品

回答

0

只是檢查的名稱後如果您的getView()方法中的圖像爲-1,則爲:

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

但是這樣你必須設置-1作爲ID的所有項目,你不想要的圖像

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> 
+0

是行不通@sumandas – Devk

+0

@Devk什麼錯誤或問題,或者問題? – sumandas

+0

我希望將「播放」圖標放在項目名稱後面。 – Devk

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:

解決下面的評論。如果你的意思是你並不需要表演圖像(OPTION 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" /> 

如果這一評論你的意思是你的意思是你不「T需要一個額外的搜索,你會已經包含了你使用你的表演圖像的一個圖像視圖。(選項2)我會改變

<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反映爲或者

OPTION 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); 

這些變化將有助於表明這是在表演圖像,你不妨顯示,然後找到你的Java代碼視圖,並給它提供一個手柄listlayout.xml定義視圖,然後確定該視圖是否應該可見並基於該視圖更改可見性屬性。

+0

我想把播放圖像只爲少數listview項目.... – Devk

+0

該播放圖像只是表明該用戶,該項目包含一個音樂文件....多數民衆贊成它...... – Devk

+0

你能請告訴我這部分如果(playImageShouldBeVisible) – Devk

0

只要做到這步:

1.與你的圖標添加按鈕隱形按鈕,您的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.適配器類中創建設置位置=新的HashSet <> ()並添加你需要的位置。

3.在getView()方法檢查:

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

使用BaseAdapter代替ArrayAdapter,只是延長BaseAdapter並實現它的方法,在getCount將()返回的列表中的項目總數,其他方法留空。 在getView()做上述 最終代碼點3寫入看起來像這樣:

`公共類適配器延伸ArrayAdapter { 私有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; 
} 

}`

它應該讓你想要什麼

+0

我在listlayout.xml中添加了按鈕.....並且在Customlist.java中添加了這個:Set positions = new HashSet <>(1,2,6);並做了你在getview()中所說的...仍然沒有運氣.....這就是我所做的一切,別的....請幫助!!!! – Devk

+0

@Devk我已經測試和編輯答案,看看 –

+0

這樣做會導致力量崩潰.....我有一個imageView和TextView的列表視圖....你沒有使用inflater它...請幫我用這個...嘗試使用我的代碼...我新開發] – Devk