我有以下代码,并且在我的片段上出现了一个列表。如何将列表项用作按钮,以便当我单击它们时,可以从ViewFlipper更改页面?
我尝试使用一个变量(changepage),它被初始化为-1。并将适配器的位置获取到其中。因此,在单击时检查它的值,并更改ViewFlipper的displaychildren函数。
我尝试使用一个变量(changepage),它被初始化为-1。并将适配器的位置获取到其中。因此,在单击时检查它的值,并更改ViewFlipper的displaychildren函数。
package com.weinco.fragments;
import java.util.ArrayList;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewFlipper;
import com.weinco.R;
public class VinocardFragment extends Fragment {
int mNum;
private ListView menuItemList;
private ListViewAdapter listViewAdapter;
public static String text;
public static ViewFlipper VF;
public static ListView listViewVino;
public static final ArrayList<String> ListviewContent = new ArrayList<String>();
public static int changepage=-1;
/**
* Create a new instance of CountingFragment, providing "num" as an
* argument.
*/
static VinocardFragment newInstance(int num) {
VinocardFragment f = new VinocardFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
// ListView
public static class ListViewAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ListViewAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return ListviewContent.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ListContent holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listviewinflate, null);
holder = new ListContent();
holder.text = (TextView) convertView
.findViewById(R.id.TextView01);
convertView.setTag(holder);
} else {
holder = (ListContent) convertView.getTag();
}
holder.text.setText(ListviewContent.get(position));
return convertView;
}
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
if (position==0){
changepage=0;
}
}
class ListContent {
TextView text;
}
}
/**
* When creating, retrieve this instance's number from its arguments.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("num") : 1;
}
/**
* The Fragment's UI is just a simple text view showing its instance number.
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.vinocard, container, false);
VF = (ViewFlipper) v.findViewById(R.id.viewFlipperVino);
OnClickListener anmeldenListener;
OnClickListener zuruckListener;
OnClickListener zuruckListener2;
// Variables
ImageButton anmeldenB = (ImageButton) v.findViewById(R.id.anmelden);
ImageButton backB = (ImageButton) v.findViewById(R.id.backVinoNr);
ImageButton backB2 = (ImageButton) v.findViewById(R.id.backVinomenu);
menuItemList = (ListView) v.findViewById(R.id.listViewVino);
EditText vE = (EditText) v.findViewById(R.id.vinonr);
text = vE.getText().toString();
getMenuItems();
// Onclick Listeners for buttons
anmeldenListener = new OnClickListener() {
@Override
public void onClick(View v) {
VF.showNext();
menuItemList.setAdapter(listViewAdapter);
}
};
if (changepage==0){
VF.showNext();
}
zuruckListener = new OnClickListener() {
@Override
public void onClick(View v) {
}
};
zuruckListener2 = new OnClickListener() {
@Override
public void onClick(View v) {
VF.showPrevious();
}
};
//Get Item ID's to pages
anmeldenB.setOnClickListener(anmeldenListener);
backB.setOnClickListener(zuruckListener);
backB2.setOnClickListener(zuruckListener2);
return v;
}
private void getMenuItems() {
ListviewContent.add("Exklusivangebote");
ListviewContent.add("Gutschein schneken");
ListviewContent.add("Kundendaten");
ListviewContent.add("Meine Gutscheine");
ListviewContent.add("Jahresbonus");
listViewAdapter = new ListViewAdapter(getActivity());
}
}
Fragments
如何工作,但您可能需要删除import com.weinco.R
语句,因为导入任何R文件都是不必要的,可能会破坏您的构建。 - Jason LListView
的id更改为“android.R.id.list”。 - Jason Landroid:id="@android:id/list"
。 - Benito Bertoli