请帮帮我。
我有ToggleButton的列表视图。
我只需要帮助我如何切换或选择listview项目。
请参见下图。
在顶部的图片中,我有心脏ToggleButton。
当我点击心脏时,它会变成红色,反之亦然。
但我不知道该怎么做。
在我点击心脏后,我想在我的收藏夹中显示那些带有心脏的项目。
这是我的适配器..
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.ImageButton;
import android.widget.TextView;
import android.widget.ToggleButton;
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);
holder.btnFavourite = (ToggleButton) convertView.findViewById(R.id.toggleButton);
holder.btnFavourite.setChecked(true);
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;
ImageButton buttonHeart;
ToggleButton btnFavourite;
}
}
这是我的xml
<?xml version="1.0" encoding="utf-8"?>
<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">
<ImageButton
android:id="@+id/mic"
android:layout_width="32dp"
android:layout_height="32dp"
android:src="@drawable/mic"
android:layout_gravity="left|top"
android:background="#ffffff" />
<ToggleButton
android:id="@+id/toggleButton"
style="@style/toggleButton"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="right|top"
android:background="@drawable/favourite_toggle_bg"/>
</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>
这是我的片段。
package com.magicstarme.virtualsongbook;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TabHost;
import java.util.ArrayList;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link FragmentOne.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link FragmentOne#newInstance} factory method to
* create an instance of this fragment.
*/
public class FragmentOne extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public FragmentOne() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment FragmentOne.
*/
// TODO: Rename and change types and number of parameters
public static FragmentOne newInstance(String param1, String param2) {
FragmentOne fragment = new FragmentOne();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@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.setId("1");
slr.setTitle("Song Title 1 Song Title 1 Title 2");
slr.setArtist("Artist 1");
slr.setVolume("Vol 1");
slr.setNumber("1");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setId("2");
slr.setTitle("Song Title 2 Song Title 2 Title 2");
slr.setArtist("Artist 2");
slr.setVolume("Vol 2");
slr.setNumber("2");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setId("3");
slr.setTitle("Song Title 3");
slr.setArtist("Artist 3");
slr.setVolume("Vol 3");
slr.setNumber("3");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setId("4");
slr.setTitle("Song Title 2");
slr.setArtist("Artist 2");
slr.setVolume("Vol 2");
slr.setNumber("4");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setId("5");
slr.setTitle("Song Title 2 Song Title 2 Title 2");
slr.setArtist("Artist 3");
slr.setVolume("Vol 3");
slr.setNumber("5");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setId("6");
slr.setTitle("Song Title 2");
slr.setArtist("Artist 2");
slr.setVolume("Vol 2");
slr.setNumber("6");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setId("7");
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("7");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setId("8");
slr.setTitle("Song Title 2");
slr.setArtist("Artist 2");
slr.setVolume("Vol 2");
slr.setNumber("8");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setId("9");
slr.setTitle("Song Title 3");
slr.setArtist("Artist 3");
slr.setVolume("Vol 3");
slr.setNumber("9");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setId("10");
slr.setTitle("Song Title 2");
slr.setArtist("Artist 2");
slr.setVolume("Vol 2");
slr.setNumber("10");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setId("11");
slr.setTitle("Song Title 3");
slr.setArtist("Artist 3");
slr.setVolume("Vol 3");
slr.setNumber("11");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setId("12");
slr.setTitle("Song Title 2");
slr.setArtist("Artist 2");
slr.setVolume("Vol 2");
slr.setNumber("12");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setId("13");
slr.setTitle("Song Title 3");
slr.setArtist("Artist 3");
slr.setVolume("Vol 3");
slr.setNumber("13");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setId("14");
slr.setTitle("Song Title 2");
slr.setArtist("Artist 2");
slr.setVolume("Vol 2");
slr.setNumber("14");
results.add(slr);
slr = new FragmentOne_slResults();
slr.setId("15");
slr.setTitle("Song Title 3");
slr.setArtist("Artist 3");
slr.setVolume("Vol 3");
slr.setNumber("15");
results.add(slr);
return results;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
我该怎么做?请帮帮我。
答案 0 :(得分:1)
要切换心脏,您可以在切换按钮上设置OnCheckedChangeListener
,如下所示:
btnFavourite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled, change the heart color to red
} else {
// The toggle is disabled, change to the default heart
}
}
});
答案 1 :(得分:0)
如果不使用POJO类,则无法正确处理列表项单击切换。
意味着您需要通过POJO类来处理这些切换以创建setter和getter并在类变量setter中更改bool值,然后当您再次滚动或重新加载列表时它会给出正确的切换值意味着它会在帮助下给出正确的响应这些
因此,请使用带有列表视图项的POJO类。
(A) In Adapter
扩展了RecyclerView.Adapter {
private ArrayList<ChildListResponse> itemsData;
viewHolder.checkParentView.setTag(itemsData.get(position));
viewHolder.checkParentView.setOnClickListener(checkedListener);
private View.OnClickListener checkedListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
ChildListResponse childListResponse = (ChildListResponse) v.getTag();
if (childListResponse.isSelected())
childListResponse.setSelected(false);
else
childListResponse.setSelected(true);
notifyDataSetChanged();
}
};