3

我有以下代码:我的片段上出现了一个列表。如何将列表项用作按钮,以便当我单击它们以从 ViewFlipper 更改页面时?
我尝试使用用-1初始化的变量(changepage)女巫。并将适配器的位置放入其中。以便单击以检查它是多少,并更改 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());

}
}
4

2 回答 2

2

您可以扩展ListFragment而不是Fragment实施onListItemClick(). 从那里,您可以获取单击的项目的 idListView并使用它做任何您想做的事情。

要实施onListItemClick()

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

在您的代码中查找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
}
于 2012-07-18T12:53:34.243 回答
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;
            }

           }
          });
于 2012-07-19T13:26:30.237 回答