当gridview添加到framelayout时,按钮不可单击

时间:2015-03-13 13:19:20

标签: android button onclicklistener android-framelayout

我有一个framelayout,中心有一个textview,底部有一个按钮(包含在bar.xml中)。该按钮在运行时完美运行。但是,我后来添加了一个gridview并使textview不可见。然而,在此之后,按钮会抓住并接触或点击事件。

任何人都知道如何实现这一目标?或者创建相同布局的替代方法。

当我从framelayout中删除按钮时,即使出现gridview,它也能正常工作。这是我的一些代码。

为result.xml

<!-- Frame 1 -->
<FrameLayout 
    android:id="@+id/result_fragment_container"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <TextView 
        android:id="@+id/result_error"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:gravity="center"
        android:visibility="gone"
        android:padding="10dp"
        android:text="No results found."
        android:textColor="@color/theme_support_color"
        android:textSize="25sp"
        android:layout_gravity="center" />      

  <include  
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_gravity="bottom"
       android:id ="@+id/result_bar"
       layout="@layout/bar" />


</FrameLayout>

和bar.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
   android:id="@+id/sl_content"
    android:padding="5dp">

<TextView 
    android:id="@+id/search_location"
    android:layout_width="0dp"
    android:layout_weight="4"
    android:layout_height="@dimen/address_height"
    android:gravity="center"
    android:textSize="16sp"
    android:textColor="#fff"
    android:singleLine="true"
    android:ellipsize="end" 
    />


    <!-- Problematic Button -->
    <Button 
        android:id="@+id/button_change_location"
        android:layout_width="0dp"
        android:layout_height="@dimen/address_change_button_height"
        android:text="Change"
        android:textSize="12sp"
        android:layout_weight="1"
        android:textColor="#fff"
        />

</LinearLayout>

从MainActivity处理代码:

 @Override
public void onVolleyDone(JSONObject response) {

    //second frame layout
    FragmentManager fragmentManager  = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    //send the shop id
    ArrayList<ListItem> listItems = new ArrayList<ListItem>();

    System.out.println("Volley done counted "+counter);
    counter++;
    //if it returned any items
    if(!response.isNull("item_0")){

        //check if the error view is visible
        if(errorView.getVisibility()==View.VISIBLE)
            errorView.setVisibility(View.GONE);

         for (int i = 0; i < response.length(); i++){
             try {

                JSONObject jObject = response.getJSONObject("item_"+i);

                String iName = jObject.getString("item_name");
                String iImage = jObject.getString("primary_image");
                int iId =  Integer.valueOf(jObject.getString("id"));

             } catch (JSONException e) {
                 e.printStackTrace();
             }
         }



         itemsFragment.setListItems(listItems);
         fragmentTransaction.add(R.id.result_fragment_container, itemsFragment); 
         fragmentTransaction.commit();


    }else{
        errorView.setVisibility(View.VISIBLE);

    }


}

上面引用的ItemsListFragment:

public class ItemsListFragment extends Fragment implements   OnItemClickListener, OnItemLongClickListener{


//variables
private ItemsListAdapter adapter;
private ArrayList<ListItem> listItems;
private GridView gridView;
private ArrayList<ListItem> tempShopItems;
private OnListItemClickListener callBack;
public static final int GRID_NUM_COLUMNS = 2;
private static final String TAG = ItemsListFragment.class.getSimpleName();
public static final int LONG_CLICK = 2;
public static final int SHORT_CLICK = 3;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View rootView = inflater.inflate(R.layout.items_list_fragment, container,false);
    return rootView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);


    //initialize other variables
    initVariables();


    //get items to display
        updateItems();

}

 @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            callBack = (OnListItemClickListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnListItemClickListener");
        }
    }



private void initVariables(){

    //shop items
    listItems = new ArrayList<ListItem>();

    //grid view
    gridView = (GridView) getActivity().findViewById(R.id.gridView);
    gridView.setOnItemLongClickListener(this);
    gridView.setNumColumns(GRID_NUM_COLUMNS);

    //adapter
    adapter = new ItemsListAdapter(getActivity().getApplicationContext(),listItems);


    //set onitemclick listener
    gridView.setOnItemClickListener(this);

    //set adapter
    gridView.setAdapter(adapter);
}


@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {


    if(parent.getItemAtPosition(position) instanceof Item){
         //start another activity

    }else
        callBack.onListItemClick(parent, view, position, id,SHORT_CLICK);
}





public void setListItems(ArrayList<ListItem> items){
    tempShopItems = items;
    System.out.println("Temp items after= "+tempShopItems.toString());

}



public void updateItems(){
    listItems.addAll(0, tempShopItems);
    adapter.notifyDataSetChanged();
}


public void removeFromList(int index){
    listItems.remove(index);
    adapter.notifyDataSetChanged();
}


@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    callBack.onListItemClick(parent, view, position, id, LONG_CLICK);
    return true;
}

public interface OnListItemClickListener{
    public void onListItemClick(AdapterView<?> parent, View view, int position,long id, int type);
}

}

0 个答案:

没有答案