无法使用列表视图单击我的底部按钮

时间:2012-12-03 08:57:18

标签: android android-listview android-fragments

这是我的my_lifts_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"
    android:orientation="vertical"
 android:background="@drawable/white_linen">

    <LinearLayout android:id="@+id/bottom_control_bar"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">
        <Button android:id="@+id/bookButton" android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:text="@string/bookRide"
            android:onClick="bookRide"/>
    </LinearLayout>

   <ListView 
      android:id="@+id/android:list" 
      android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_above="@id/bottom_control_bar"
      android:divider="#b5b5b5"
      android:dividerHeight="3dp"
      android:listSelector="@drawable/custom_list_selector"/>
   <TextView
    android:id="@+id/android:empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="You have not joined any rides."/>     
</RelativeLayout>

这是我的片段活动:

public class LiftActivity extends FragmentActivity {

    private static final String TAG = LiftActivity.class.getName();

    private ArrayList<Lift> mLifts = new ArrayList<Lift>();

    private LiftArrayAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_lift_list);

        mAdapter = new LiftArrayAdapter(this, R.layout.ride_item_card, mLifts);

        FragmentManager     fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        LiftListFragment list = new LiftListFragment();
        ft.add(android.R.id.content, list);

        list.setListAdapter(mAdapter);

        MyLiftsResponderFragment responder = (MyLiftsResponderFragment) fm.findFragmentByTag("RESTResponder");

        if (responder == null) {
            responder = new MyLiftsResponderFragment(); 
            ft.add(responder, "RESTResponder");
        }

        ft.commit();
    }

    public void bookRide(View v){
        Intent intent = new Intent(this, LiftSearchActivity.class);
        startActivity(intent);
    }

    public LiftArrayAdapter getArrayAdapter() {
        return mAdapter;
    }
}

我的liftlistfragment:

public class LiftListFragment extends ListFragment {

    private static final String TAG = LiftListFragment.class.getName();

    private static final int ACTIVITY_VIEW_LIFT = 0;


    @Override
    public void onListItemClick(ListView list, View v, int position, long id) {
        Lift item = (Lift) getListAdapter().getItem(position);
        Intent intent = new Intent(this.getActivity(), LiftDetailActivity.class);
        intent.putExtra("lift", item.getId());
        intent.putExtra("time", item.getTime());
        intent.putExtra("price", item.getPrice());
        startActivity(intent);
    }

}

最后,我的电梯列表适配器:

public class LiftArrayAdapter extends ArrayAdapter<Lift> {

    private final ArrayList<Lift> lifts;

    public LiftArrayAdapter(Context context, int textViewResourceId, ArrayList<Lift> results) {
        super(context, textViewResourceId, results);
        this.lifts = results;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // assign the view we are converting to a local variable
        View v = convertView;

        // first check to see if the view is null. if so, we have to inflate it.
        // to inflate it basically means to render, or show, the view.
        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.ride_item_card, null);
        }

        Lift i = lifts.get(position);

        if (i != null) {

            ImageView imageView = (ImageView) v.findViewById(R.id.list_image);

            TextView departureDate = (TextView) v.findViewById(R.id.departureDate);
            TextView driverName = (TextView) v.findViewById(R.id.driverName);
            TextView placesRemaining = (TextView) v.findViewById(R.id.placesRemaining);
            TextView ridePrice = (TextView) v.findViewById(R.id.ridePrice);

            imageView.setTag(i.getDriver().getImage());

            ImageLoader image = new ImageLoader();
            image.execute(imageView);

            if (departureDate != null){
                departureDate.setText(i.getFrom().getDescription() + " vers " + i.getTo().getDescription());
            }
            if (driverName != null){
                driverName.setText(i.getTime());
            }
            if (placesRemaining != null){
                placesRemaining.setText("Places remaining : " + Integer.toString(i.getCapacity() - i.getPassengers().size()));
            }
            if (ridePrice != null){
                ridePrice.setText("$" + Float.toString(i.getPrice()));

            }
        }

        // the view must be returned to our activity
        return v;

    }
}

所以基本上,我有我的项目列表(可以点击),我的按钮在底部是不可点击的。

我不明白为什么我不能让这个按钮工作。如果我使用键盘,我可以选择它。我认为它必须是一些片段问题我的listfragment和我的activityfragment之间的onclicklistener用法(所以,可能我的按钮充当项目)。有没有更好的方法来实现这个?

1 个答案:

答案 0 :(得分:1)

您为活动设置了布局文件,然后将LiftListFragment片段直接添加到FragmentActivitiy的内容Framelayout(ID为android.R.id.content),其中将涵盖整个之前设置的布局。您确定不希望LiftListFragment布局文件中添加my_lifts_list.xml吗?

<RelativeLayout>
   // the button part
   <FrameLayout android:id="@+id/liftfrag_wrapper" android:layout_above="@id/the_button" // other attributes/>
</RelativeLayout>

然后在你的代码中:

ft.add(R.id.liftfrag_wrapper, list);