为什么菜单抽屉上的项目无法点击? Android导航

时间:2017-08-16 08:21:27

标签: java android android-fragments stack-overflow

我的Navigator菜单有一个非常有趣的问题。我不知道为什么......但是我可以点击我菜单中的任何项目,我不想说我点击而没有任何事情发生。我真的想说我不能点击任何项目,我的所有菜单都像一个大图像。我尝试制作一个已经有导航抽屉活动的新项目,当然它可以工作..但是当我尝试复制那些代码然后放上我的...我有同样的问题,反之亦然,我尝试将我的代码放入带有导航抽屉活动的新项目中,但是再次......我无法点击任何项目。

图片 - this trouble

图片 - can't click

这是我的代码:

menu_bar.java

package com.database.m.lyburan;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.database.m.lyburan.adapter.EventAdapter;
import com.database.m.lyburan.app.AppController;
import com.database.m.lyburan.data.EventData;
import com.database.m.lyburan.util.Server;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.database.m.lyburan.Buddies.TAG_MESSAGE;
import static com.database.m.lyburan.Login.TAG_USERNAME;
import static com.database.m.lyburan.R.id.parent;

public class menu_bar extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, SwipeRefreshLayout.OnRefreshListener {

Toolbar toolbar;
DrawerLayout drawer;
NavigationView navigationView;
FragmentManager fragmentManager;
Fragment fragment = null;

ListView list;
SharedPreferences sharedpreferences;
SwipeRefreshLayout swipe;
List<EventData> eventList = new ArrayList<EventData>();

private static final String TAG = menu_bar.class.getSimpleName();

private static String url_list   = Server.URL + "event.php?offset=";

private int offSet = 0;

int no;

EventAdapter adapter;

public static final String TAG_NO       = "no";
public static final String TAG_ID       = "id";
public static final String TAG_JUDUL    = "judul";
public static final String TAG_TGL      = "tgl";
public static final String TAG_ISI      = "isi";
public static final String TAG_GAMBAR   = "gambar";

Handler handler;
Runnable runnable;

private Boolean isFabOpen = false;
private FloatingActionButton fab, fab1, fab2;
private CardView card1, card2;
private Animation fab_open, fab_close, rotate_forward, rotate_backward, card_open, card_close;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu_bar);



    sharedpreferences = getSharedPreferences(Login.my_shared_preferences, Context.MODE_PRIVATE);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    drawer = (DrawerLayout) findViewById(R.id.drawer_layouts);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    if (savedInstanceState == null) {
        fragment = new Root();
        callFragment(fragment);
    }

    swipe = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
    list = (ListView) findViewById(R.id.list_event);
    eventList.clear();

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Intent intent = new Intent(menu_bar.this, Buddies.class);
            intent.putExtra(TAG_ID, eventList.get(position).getId());
            startActivity(intent);
        }
    });

    adapter = new EventAdapter(menu_bar.this, eventList);
    list.setAdapter(adapter);

    swipe.setOnRefreshListener(this);

    swipe.post(new Runnable() {
                   @Override
                   public void run() {
                       swipe.setRefreshing(true);
                       eventList.clear();
                       adapter.notifyDataSetChanged();
                       callEvent(0);
                   }
               });

    list.setOnScrollListener(new AbsListView.OnScrollListener() {

        private int currentVisibleItemCount;
        private int currentScrollState;
        private int currentFirstVisibleItem;
        private int totalItem;

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            this.currentScrollState = scrollState;
            this.isScrollCompleted();
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            this.currentFirstVisibleItem = firstVisibleItem;
            this.currentVisibleItemCount = visibleItemCount;
            this.totalItem = totalItemCount;
        }

        private void isScrollCompleted() {
            if (totalItem - currentFirstVisibleItem == currentVisibleItemCount
                    && this.currentScrollState == SCROLL_STATE_IDLE) {

                swipe.setRefreshing(true);
                handler = new Handler();

                runnable = new Runnable() {
                    public void run() {
                        callEvent(offSet);
                    }
                };

                handler.postDelayed(runnable, 3000);
            }
        }

    });

    Button meet_buddies = (Button) findViewById(R.id.meet_buddies);
    meet_buddies.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(menu_bar.this,Buddies.class);
            startActivity(intent);
        }
    });

    Button upcoming = (Button) findViewById(R.id.upcoming);
    upcoming.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(menu_bar.this,asd.class);
            startActivity(intent);
        }
    });


    fab = (FloatingActionButton) findViewById(R.id.fab);
    fab1 = (FloatingActionButton) findViewById(R.id.fab1);
    fab2 = (FloatingActionButton) findViewById(R.id.fab2);
    card1 = (CardView) findViewById(R.id.card1);
    card2 = (CardView) findViewById(R.id.card2);

    /*kenalkan animasi*/
    fab_open = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fab_open);
    fab_close = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fab_close);
    rotate_forward = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate_forward);
    rotate_backward = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate_backward);
    card_open=AnimationUtils.loadAnimation(getApplicationContext(), R.anim.card_open);
    card_close=AnimationUtils.loadAnimation(getApplicationContext(), R.anim.card_close);

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            animateFAB();
        }
    });

    fab1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(menu_bar.this,Addhome.class);
            startActivity(intent);
        }
    });

    fab2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(menu_bar.this, "New Call", Toast.LENGTH_SHORT).show();
        }
    });
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_bar, menu);
    return true;
}

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_profile) {
        fragment = new Import();
        callFragment(fragment);
    } else if (id == R.id.nav_payment) {
        fragment = new Import();
        callFragment(fragment);
    } else if (id == R.id.nav_help) {
        fragment = new Import();
        callFragment(fragment);
    } else if (id == R.id.btn_logout) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layouts);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        Toast.makeText(getApplicationContext(), "Action Settings", Toast.LENGTH_SHORT).show();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

private void callFragment(Fragment fragment) {
    fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.frame_containers, fragment)
            .commit();
}

public void animateFAB() {
    /*jika fab dalam keadaan false*/
    if (isFabOpen) {
        fab.startAnimation(rotate_backward);
        fab1.startAnimation(fab_close);
        fab2.startAnimation(fab_close);
        card1.startAnimation(card_close);
        card2.startAnimation(card_close);
        fab1.setClickable(false);
        fab2.setClickable(false);
        isFabOpen = false;
    } else {
        /*jika dalam keadaan true*/
        fab.startAnimation(rotate_forward);
        fab1.startAnimation(fab_open);
        fab2.startAnimation(fab_open);
        card1.startAnimation(card_open);
        card2.startAnimation(card_open);
        fab1.setClickable(true);
        fab2.setClickable(true);
        isFabOpen = true;
    }
}

@Override
public void onRefresh() {
    eventList.clear();
    adapter.notifyDataSetChanged();
    callEvent(0);
}

private void callEvent(int page){

    swipe.setRefreshing(true);

    JsonArrayRequest arrReq = new JsonArrayRequest(url_list + page,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());

                    if (response.length() > 0) {
                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {

                                JSONObject obj = response.getJSONObject(i);
                                EventData news = new EventData();

                                no = obj.getInt(TAG_NO);

                                news.setId(obj.getString(TAG_ID));
                                news.setJudul(obj.getString(TAG_JUDUL));

                                if (!Objects.equals(obj.getString(TAG_GAMBAR), "")) {
                                    news.setGambar(obj.getString(TAG_GAMBAR));
                                }

                                news.setDatetime(obj.getString(TAG_TGL));
                                news.setIsi(obj.getString(TAG_ISI));

                                // adding news to news array
                                eventList.add(news);

                                if (no > offSet)
                                    offSet = no;

                                Log.d(TAG, "offSet " + offSet);

                            } catch (JSONException e) {
                                Log.e(TAG, "JSON Parsing error: " + e.getMessage());
                            }

                            adapter.notifyDataSetChanged();
                        }
                    }
                    swipe.setRefreshing(false);
                }

            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            swipe.setRefreshing(false);
        }
    });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(arrReq);
}
}

activity_menu_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layouts"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_menu_bar"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_menu_bar"
        app:menu="@menu/activity_menu_bar_drawer" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="16dp"
        android:paddingRight="16dp">


        <Button
            android:id="@+id/meet_buddies"
            android:layout_width="fill_parent"
            android:drawableLeft="@drawable/ic_profil_w"
            android:background="@color/ijo"
            android:textColor="@android:color/background_light"
            android:layout_height="wrap_content"
            android:padding="15dp"
            android:onClick="meet"
            android:layout_marginTop="70dp"
            android:text="Meet Buddies"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <Button
            android:id="@+id/upcoming"
            android:layout_width="170dp"
            android:background="@color/white"
            android:textColor="@color/ijo"
            android:layout_height="wrap_content"
            android:padding="15dp"
            android:text="Notification"
            android:layout_marginTop="135dp" />

        <Button
            android:id="@+id/myshared"
            android:layout_width="190dp"
            android:background="@color/white"
            android:textColor="@color/ijo"
            android:layout_height="wrap_content"
            android:padding="15dp"
            android:layout_marginLeft="190dp"
            android:layout_marginTop="135dp"
            android:text="My Shared"
             />



    </RelativeLayout>
    <android.support.design.widget.CoordinatorLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="10dp">



        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            android:stateListAnimator="@null"
            android:src="@drawable/ic_plus_w"
            app:backgroundTint="@color/colorAccent"
            app:elevation="6dp"
            app:pressedTranslationZ="12dp" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_marginBottom="130dp"
            android:stateListAnimator="@null"
            android:layout_marginRight="@dimen/fab_margin"
            android:visibility="invisible"
            android:src="@drawable/ic_menu_camera"
            app:backgroundTint="#ffffff"
            app:elevation="6dp"
            app:pressedTranslationZ="12dp" />

        <android.support.v7.widget.CardView
            android:id="@+id/card2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_marginBottom="140dp"
            android:layout_marginRight="75dp"
            android:visibility="invisible">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:text="Add Event" />
        </android.support.v7.widget.CardView>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_marginBottom="80dp"
            android:layout_marginRight="@dimen/fab_margin"
            android:src="@drawable/ic_beranda"
            android:visibility="invisible"
            app:backgroundTint="#ffffff"
            app:elevation="6dp"
            app:pressedTranslationZ="12dp" />

        <android.support.v7.widget.CardView
            android:id="@+id/card1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_marginBottom="90dp"
            android:layout_marginRight="70dp"
            android:visibility="invisible">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:text="Add Homestay" />
        </android.support.v7.widget.CardView>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Event"
        android:textStyle="bold"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="198dp" />

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_marginTop="222dp"
        android:layout_height="wrap_content">

        <ListView
            android:id="@+id/list_event"
            android:layout_width="wrap_content"
            android:layout_height="330dp"
            android:divider="@null"
            android:dividerHeight="2dp"
            android:layout_marginTop="222dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

    </android.support.v4.widget.SwipeRefreshLayout>

    </RelativeLayout>
    </android.support.design.widget.CoordinatorLayout>

</android.support.v4.widget.DrawerLayout>

我已经尝试过,我该如何解决?有人请帮助我。感谢。

3 个答案:

答案 0 :(得分:0)

这个adapter.java

public class Adapter extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater;
    private List<DataModel> item;

    public Adapter(Activity activity, List<DataModel> item) {
        this.activity = activity;
        this.item = item;
    }

    @Override
    public int getCount() {
        return item.size();
    }

    @Override
    public Object getItem(int location) {
        return item.get(location);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null)
            convertView = inflater.inflate(R.layout.list_buddies, null);

        TextView txt_nama = (TextView) convertView.findViewById(R.id.custom_list_item_text1);

        txt_nama.setText(item.get(position).getNama());

        return convertView;
    }
}

答案 1 :(得分:0)

您需要创建此类型的布局xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
        android:layout_width="match_parent" android:layout_height="match_parent"
        android:fitsSystemWindows="true" tools:openDrawer="start">

        <FrameLayout
            android:id="@+id/container"
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true">

        </FrameLayout>

         <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_menu_bar"
            app:menu="@menu/activity_menu_bar_drawer" />

    </android.support.v4.widget.DrawerLayout>

FrameLayout 中,您需要逐个替换所有片段,如

private void callFragment(Fragment fragment) {
    fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, fragment)
            .commit();
}

注意:您可以编写协调器布局和其他片段本身,可能对您有所帮助。

答案 2 :(得分:0)

其他有类似问题的人。看来XML中的顺序也很重要。

我有了我的NavigationView,之后我有一个布局,我无法点击NavigationView上的一个项目。

在我切换它们之后,我的布局被包含在内,然后我有了我的NavigationView,它有效...