(Android)如何使用listView作为按钮?

时间:2012-07-18 12:48:04

标签: android listview button android-fragments viewflipper

我有以下代码:我的片段上会出现一个列表。我如何使用列表项作为按钮,以便当我单击它们以从ViewFlipper更改页面时?
我尝试使用变量(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());

}
}

2 个答案:

答案 0 :(得分:2)

您可以展开ListFragment而不是Fragment并实施onListItemClick()。从那里,你可以获得点击ListView项目的ID,并随意做任何事情。

实施onListItemClick()

  1. 右键单击代码空间。
  2. 鼠标悬停“来源”
  3. 点击“覆盖/实施方法”
  4. 查找并查看onListItemClick(ListView l, View v, int position, long id)
  5. 点击“确定”以实施检查方法。
  6. 在代码中找到onListItemClick,您应该看到:

    public void onListItemClick (ListView l, View v, int position, long id)
    {
       // l - ListView that was clicked
       // v - View that was clicked within ListView l
       // position - Position of View v within ListView l
       // id - row id of item that was clicked
    }
    

答案 1 :(得分:0)

这解决了它:

menuItemList.setOnItemClickListener(new OnItemClickListener(){

           public void onItemClick(AdapterView<?> menuItemList, View v, int position,
             long id) {
           changepageothers=position;
            if (changepageothers==0){              
             VF.showNext();
             changepageothers=-1;
            }
            if (changepageothers==1){
             VF.showPrevious();
             changepageothers=-1;
            }

           }
          });