请帮帮我。
我用 ImageView ListView 。我想在点击心形图像时将心脏图像的颜色更改为红色。然后我要显示所有点击的项目在favorites.it就像书签ListView项目
这是输出。
这是我的代码。
FragmentOne
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_fragment_one, container, false);
TabHost host = (TabHost) rootView.findViewById(R.id.tabHost);
host.setup();
//Tab 1
TabHost.TabSpec spec = host.newTabSpec("SONG LIST");
spec.setContent(R.id.tab1);
spec.setIndicator("SONG LIST");
host.addTab(spec);
ArrayList<FragmentOne_slResults> slResults = GetSearchResults();
final ListView sllv = (ListView) rootView.findViewById(R.id.slListView);
sllv.setAdapter(new FragmentOne_Adapter(getActivity(), slResults));
//Tab 2
spec = host.newTabSpec("NEW SONGS");
spec.setContent(R.id.tab2);
spec.setIndicator("NEW SONGS");
host.addTab(spec);
//Tab 3
spec = host.newTabSpec("FAVORITES");
spec.setContent(R.id.tab3);
spec.setIndicator("FAVORITES");
host.addTab(spec);
return rootView;
}
//SONG LIST
private ArrayList<FragmentOne_slResults> GetSearchResults(){
ArrayList<FragmentOne_slResults> results = new ArrayList<FragmentOne_slResults>();
FragmentOne_slResults slr = new FragmentOne_slResults();
slr.setTitle("Song Title 1 Song Title 1 Title 2");
slr.setArtist("Artist 1");
slr.setVolume("Vol 1");
slr.setNumber("123456");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setTitle("Song Title 2 Song Title 2 Title 2");
slr.setArtist("Artist 2");
slr.setVolume("Vol 2");
slr.setNumber("12356");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setTitle("Song Title 3");
slr.setArtist("Artist 3");
slr.setVolume("Vol 3");
slr.setNumber("12456");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setTitle("Song Title 2");
slr.setArtist("Artist 2");
slr.setVolume("Vol 2");
slr.setNumber("1256");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setTitle("Song Title 2 Song Title 2 Title 2");
slr.setArtist("Artist 3");
slr.setVolume("Vol 3");
slr.setNumber("1233");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setTitle("Song Title 2");
slr.setArtist("Artist 2");
slr.setVolume("Vol 2");
slr.setNumber("12456");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setTitle("Song Title 2 Song Title 2 Title 2 Song Title 2 Song Title 2 Title 23 tle 2 Song Title 2 Song Title 2 Title 2");
slr.setArtist("Artist 3");
slr.setVolume("Vol 3");
slr.setNumber("123456");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setTitle("Song Title 2");
slr.setArtist("Artist 2");
slr.setVolume("Vol 2");
slr.setNumber("123456");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setTitle("Song Title 3");
slr.setArtist("Artist 3");
slr.setVolume("Vol 3");
slr.setNumber("123456");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setTitle("Song Title 2");
slr.setArtist("Artist 2");
slr.setVolume("Vol 2");
slr.setNumber("1256");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setTitle("Song Title 3");
slr.setArtist("Artist 3");
slr.setVolume("Vol 3");
slr.setNumber("123456");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setTitle("Song Title 2");
slr.setArtist("Artist 2");
slr.setVolume("Vol 2");
slr.setNumber("123456");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setTitle("Song Title 3");
slr.setArtist("Artist 3");
slr.setVolume("Vol 3");
slr.setNumber("123456");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setTitle("Song Title 2");
slr.setArtist("Artist 2");
slr.setVolume("Vol 2");
slr.setNumber("123456");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setTitle("Song Title 3");
slr.setArtist("Artist 3");
slr.setVolume("Vol 3");
slr.setNumber("1236");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setTitle("Song Title 2");
slr.setArtist("Artist 2");
slr.setVolume("Vol 2");
slr.setNumber("123456");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setTitle("Song Title 3");
slr.setArtist("Artist 3");
slr.setVolume("Vol 3");
slr.setNumber("123456");
results.add(slr);
return results;
}
FragmentOne_Adapter
package com.magicstarme.virtualsongbook;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by Joe on 6/29/2016.
*/
public class FragmentOne_Adapter extends BaseAdapter {
private static ArrayList<FragmentOne_slResults> oneslArrayList;
private LayoutInflater mInflater;
public FragmentOne_Adapter(Context context, ArrayList<FragmentOne_slResults> slresults) {
oneslArrayList = slresults;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return oneslArrayList.size();
}
public Object getItem(int position) {
return oneslArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.fragment_fragment_one_slview, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.txtArtist = (TextView) convertView.findViewById(R.id.artist);
holder.txtVolume = (TextView) convertView.findViewById(R.id.volume);
holder.txtNumber = (TextView) convertView.findViewById(R.id.number);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtTitle.setText(oneslArrayList.get(position).getTitle());
holder.txtArtist.setText(oneslArrayList.get(position).getArtist());
holder.txtVolume.setText(oneslArrayList.get(position).getVolume());
holder.txtNumber.setText(oneslArrayList.get(position).getNumber());
return convertView;
}
static class ViewHolder {
TextView txtTitle;
TextView txtArtist;
TextView txtVolume;
TextView txtNumber;
}
}
这是xml
<?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"
android:orientation="horizontal"
android:paddingLeft="10dip" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="200dp"
android:layout_height="match_parent"
android:id="@+id/relativeLayout1"
android:paddingRight="5dip">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17dp"
android:textStyle="bold" />
</FrameLayout>
<FrameLayout
android:id="@+id/framelayout2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/volume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_gravity="right|top" />
<TextView
android:id="@+id/artist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_gravity="left|top" />
</FrameLayout>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="65dp"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:id="@+id/relativeLayout2"
android:layout_toStartOf="@+id/relativeLayout3">
<FrameLayout
android:layout_width="65dp"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:paddingTop="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/mic"
android:layout_gravity="left|top" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/heart"
android:layout_gravity="right|top" />
</FrameLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="80dp"
android:layout_height="match_parent"
android:background="#cccccc"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:id="@+id/relativeLayout3">
<FrameLayout
android:layout_width="80dp"
android:layout_height="match_parent"
android:padding="5dp"
android:background="#cccccc">
<TextView
android:id="@+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal|top" />
</FrameLayout>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:paddingRight="5dip"
android:orientation="vertical"
android:layout_weight="3
">
<FrameLayout
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17dp"
android:textStyle="bold" />
</FrameLayout>
<FrameLayout
android:id="@+id/framelayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/artist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_gravity="left|top"/>
<TextView
android:id="@+id/volume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_gravity="right|top"/>
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:layout_toStartOf="@+id/relativeLayout3">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:padding="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/mic"
android:layout_gravity="left|top" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/heart"
android:layout_gravity="right|top" />
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#cccccc"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:background="#cccccc">
<TextView
android:id="@+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal|top" />
</FrameLayout>
</LinearLayout>
</LinearLayout>
请帮帮我。
我正在开发一个歌剧应用程序,如来自Playstore的数字歌本。
感谢,
乔
答案 0 :(得分:2)
您是否尝试在Viewholder中将其添加为ImageButton,然后在getView中为其添加SetOnclickListener?
holder.button = (ImageButton)convertView.findviewById( ... );
final ImageButton tmp = holder.button;
tmp.setOnClickListenet( ... ) ;
编辑: 在您的XML而不是:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/heart"
android:layout_gravity="right|top" />
将其替换为:
<ImageButton
android:id < add your ID here !!! >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/heart"
android:layout_gravity="right|top" />
答案 1 :(得分:1)
这是我改变心脏颜色的代码。在你的布局xml文件中使用这个
<ToggleButton
android:id="@+id/toggleButton"
style="@style/toggleButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="right"
android:background="@drawable/favourite_toggle_bg"/>
在样式文件中添加此内容。
<style name="toggleButton" parent="@android:Theme.Black">
<item name="android:buttonStyleToggle">@style/Widget.Button.Toggle</item>
<item name="android:textOn"></item>
<item name="android:textOff"></item>
</style>
在drawable文件夹的favourite_toggle_bg.xml中创建新文件并添加此代码。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:drawable="@mipmap/favourite_no" />
<item android:state_checked="true"
android:drawable="@mipmap/favourite_yes" />
</selector>
将ToggleButton btnFavourite添加到你的持有人中并使用ToggleButton启动它
holder.btnFavourite = (ToggleButton) convertView.findViewById(R.id.toggleButton);
在java代码中,如果你想收藏Toggle按钮检查,那么在你的getView适配器方法中添加它
holder.btnFavourite.setChecked(true);