在侧边菜单中实现 ListView

Implementaing ListView within sidemenu

这是我用来实现滑动面板的程序代码,但我想在其中制作列表视图项目。那我该怎么做呢?请帮忙。 提前致谢! 主要活动代码:

// Slide the Panel
    menuRightButton = (ImageView) findViewById(R.id.menuViewButton);
    menuRightButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!isExpanded) {
                isExpanded = true;
                // Expand

                FragmentManager fragmentManager = getFragmentManager();

                FragmentTransaction fragmentTransaction = fragmentManager
                        .beginTransaction();
                fragmentTransaction.replace(R.id.menuPanel,
                        new LeftMenuFragment());
                fragmentTransaction.commit();
                new ExpandAnimation(slidingPanel, panelWidth1,
                        Animation.RELATIVE_TO_SELF, 0.0f,
                        Animation.RELATIVE_TO_SELF, 0.55f, 0, 0.0f, 0, 0.0f);

            } else {
                isExpanded = false;
                // Collapse

                new CollapseAnimation(slidingPanel, panelWidth1,
                        TranslateAnimation.RELATIVE_TO_SELF, 0.55f,
                        TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f,
                        0, 0.0f);

            }
        }
    });

Leftmenufragement.java

//Left Menu   
public class LeftMenuFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.leftmenu, container, false);
}

}

leftmenu.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#2C323F"
 >

<ListView
    android:id="@+id/listview2"
    android:layout_width="250dp"
    android:layout_height="550dp"
    android:background="#32394A"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"/>

将您的 LeftMenuFragment 更新为:

public class LeftMenuFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

     // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.leftmenu, container, false);
    ListView menuList = (ListView) view.findViewById(R.id.listview2);
  String[] items = { "Home", "Setting"};

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, items);

    menuList .setAdapter(adapter); 
    return view;
}

}
DashboardActivity.java


    import android.app.Fragment;
    import android.app.FragmentManager;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.view.GravityCompat;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.android.volley.AuthFailureError;
    import com.android.volley.NetworkError;
    import com.android.volley.NoConnectionError;
    import com.android.volley.ParseError;
    import com.android.volley.Request;
    import com.android.volley.RequestQueue;
    import com.android.volley.Response;
    import com.android.volley.ServerError;
    import com.android.volley.TimeoutError;
    import com.android.volley.VolleyError;
    import com.android.volley.toolbox.StringRequest;
    import com.android.volley.toolbox.Volley;
    import com.bumptech.glide.Glide;

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

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;

    import butterknife.BindView;
    import butterknife.ButterKnife;
    import butterknife.OnClick;
    import de.hdodenhof.circleimageview.CircleImageView;

    public class DashboardActivity extends AppCompatActivity {
        @BindView(R.id.txt_userName)
        TextView txtUserName;
        @BindView(R.id.txt_city)
        TextView txtCity;
        @BindView(R.id.profile_image)
        CircleImageView profileImage;

        //HandburgerMenu

        private ListView mDrawerList;
        private LinearLayout mDrawerPane;
        private ActionBarDrawerToggle mDrawerToggle;
        private DrawerLayout mDrawerLayout;
        private ImageView toggle;
        private ImageView profile_image;
        private ArrayList<NavItem> mNavItems = new ArrayList<>();

        SessionManager mSessionManager;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_dashboard);
            ButterKnife.bind(this);


            mSessionManager = new SessionManager(DashboardActivity.this);
            //HandburgerMenu ----------------------------------------------------------
            toggle = findViewById(R.id.toggle);

            mNavItems.add(new NavItem("Products", "", R.drawable.products));
            mNavItems.add(new NavItem("Saved Resume", "", R.drawable.my_resume));
            mNavItems.add(new NavItem("My Resume", "", R.drawable.my_resume));
            mNavItems.add(new NavItem("My Documents", "", R.drawable.my_docu));
            mNavItems.add(new NavItem("Interview Preparation", "", R.drawable.interview));
            mNavItems.add(new NavItem("About Us", "", R.drawable.about));
            mNavItems.add(new NavItem("My Blogs", "", R.drawable.blogs));
            mNavItems.add(new NavItem("All Blogs", "", R.drawable.blogs));
            mNavItems.add(new NavItem("Settings", "", R.drawable.settings));
            mNavItems.add(new NavItem("Logout", "", R.drawable.logout));

            // DrawerLayout
            mDrawerLayout = findViewById(R.id.drawerLayout);

            // Populate the Navigtion Drawer with options
            mDrawerPane = findViewById(R.id.drawerPane);
            mDrawerList = findViewById(R.id.navList);
            // mDrawerList.setDividerHeight(20);
            DrawerListAdapter adapter = new DrawerListAdapter(this, mNavItems);
            mDrawerList.setAdapter(adapter);

            // Drawer Item click listeners
            mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    selectItemFromDrawer(position);
                }
            });


            mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
                @Override
                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);

                    invalidateOptionsMenu();


                    //Called when drawer is opened

                }

                @Override
                public void onDrawerClosed(View drawerView) {
                    super.onDrawerClosed(drawerView);

                    //Called when drawer is Closed
                    invalidateOptionsMenu();
                }
            };

            mDrawerLayout.setDrawerListener(mDrawerToggle);


            toggle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
                        mDrawerLayout.openDrawer(GravityCompat.START);


                        SetDrawerDetails();

                    } else {
                        mDrawerLayout.closeDrawer(mDrawerPane);
                    }
                }
            });
            //-------------------------------------------------------------------------------------------


            mDrawerList.setItemChecked(0, true);
            setTitle(mNavItems.get(0).mTitle);


        }



        //Method to set drawer details
        public void SetDrawerDetails() {

            txtUserName.setText(mSessionManager.getStringData("USERNAME") + " " + mSessionManager.getStringData("USER_SURNAME"));
            txtCity.setText(mSessionManager.getStringData("USER_CITY"));

            try {
                Log.e("USER_IMAGE", "USER_IMAGE " + mSessionManager.getStringData("USER_IMAGE"));
                Glide.with(this).load(mSessionManager.getStringData("USER_IMAGE")).into(profileImage);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        /*
         * Called when a particular item from the navigation drawer
         * is selected.
         * */
        private void selectItemFromDrawer(int position) {
            Fragment fragment = null;
            Boolean IsInMeters = false;
            switch (position) {
                case 0:
                    // fragment = new ProductsFragment();
                    break;
                case 1:
                    //  fragment = new SavedResumeFragment();

                    break;
                case 2:
                    //   fragment = new MyResumeFragment();

                    break;
                case 3:
                    //  fragment = new MyDocFragment();


                    break;
                case 4:

                    //  fragment = new InteriewPreparationFragment();


                    break;
                case 5:
                    fragment = new AboutUsFragment();

                    break;
                case 6:
                    //   fragment = new BlogsFragment();

                    break;
                case 7:
                    //   fragment = new BlogsFragment("All");

                    break;
                case 8:
                    //      fragment = new ChangePasswordFragment();

                    break;
                case 9:

                    mSessionManager.removeData("USERID");
                    mSessionManager.removeData("USERNAME");
                    mSessionManager.removeData("USER_MIDDLE_NAME");
                    mSessionManager.removeData("USER_SURNAME");
                    mSessionManager.removeData("USER_FULLNAME");
                    mSessionManager.removeData("USER_ADDRESS");
                    mSessionManager.removeData("USER_MOBILE");
                    mSessionManager.removeData("USER_CITY");
                    mSessionManager.removeData("USER_STATE");
                    mSessionManager.removeData("USER_COUNTRY");
                    mSessionManager.removeData("USER_DOB");
                    mSessionManager.removeData("USER_EMAIL");
                    mSessionManager.removeData("USER_IMAGE");
                    mSessionManager.removeData("USER_STATUS");
                    //  startActivity(new Intent(DashboardActivity.this, LoginActivity.class));
                    finish();
                    break;
                default:

            }


            if (position != 9) {
                if (fragment != null) {
                    FragmentManager fragmentManager = getFragmentManager();
                    fragmentManager.beginTransaction()
                            .replace(R.id.mainContent, fragment)
                            .addToBackStack(mNavItems.get(position).mTitle)
                            .commit();
                }
            }

            mDrawerList.setItemChecked(position, true);
            setTitle(mNavItems.get(position).mTitle);

            // Close the drawer
            mDrawerLayout.closeDrawer(mDrawerPane);
        }


        @Override
        public void onBackPressed() {
            // super.onBackPressed();


            if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
                mDrawerLayout.closeDrawer(GravityCompat.START);
            } else {

                FragmentManager fragmentManager = getFragmentManager();
                if (fragmentManager.getBackStackEntryCount() > 1) {

                    fragmentManager.popBackStack();
                } else {


                    finish();

                }
                Log.e("popping BACKSTRACK===> ", "" + fragmentManager.getBackStackEntryCount());
            }
        }


    }





DrawerListAdapter.java



    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;

    import java.util.ArrayList;

    public class DrawerListAdapter extends BaseAdapter {

        private Context mContext;
        private ArrayList<NavItem> mNavItems;

        public DrawerListAdapter(Context context, ArrayList<NavItem> navItems) {
            mContext = context;
            mNavItems = navItems;
        }

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

        @Override
        public Object getItem(int position) {
            return mNavItems.get(position);
        }

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

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

            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.drawer_item, null);
            } else {
                view = convertView;
            }

            TextView titleView = view.findViewById(R.id.title);
            TextView subtitleView = view.findViewById(R.id.subTitle);
            ImageView iconView = view.findViewById(R.id.icon);

            titleView.setText(mNavItems.get(position).mTitle);
            subtitleView.setText(mNavItems.get(position).mSubtitle);
            iconView.setImageResource(mNavItems.get(position).mIcon);

            return view;
        }
    }


NavItem.java

    public class NavItem {
    public String mTitle;
    public String mSubtitle;
    public int mIcon;

    public NavItem(String title, String subtitle, int icon) {
        mTitle = title;
        mSubtitle = subtitle;
        mIcon = icon;
    }
    }



AboutUsFragment.java



    import android.app.Fragment;
    import android.app.ProgressDialog;
    import android.graphics.Bitmap;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.LayoutInflater;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.ViewGroup;
    import android.webkit.WebChromeClient;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;

    import butterknife.BindView;
    import butterknife.ButterKnife;
    import butterknife.Unbinder;

    import static com.dev.sidemenudemo.Constants.URL_BASE_WEB;


    public class AboutUsFragment extends Fragment {

        @BindView(R.id.webView)
        WebView webView;
        Unbinder unbinder;

        public AboutUsFragment() {
        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.fragment_about_us, container, false);

            unbinder = ButterKnife.bind(this, view);


            webView.setWebViewClient(new WebViewClient());    //the lines of code added
            webView.setWebChromeClient(new WebChromeClient()); //same as above
            webView.getSettings().setJavaScriptEnabled(true);
            webView.loadUrl(URL_BASE_WEB + "about-us");


            final ProgressDialog progressBar = new ProgressDialog(getActivity());
            progressBar.setMessage("Please wait...");


            webView.setWebViewClient(new WebViewClient() {
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }

                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    super.onPageStarted(view, url, favicon);
                    if (!progressBar.isShowing()) {
                        progressBar.show();
                    }
                }

                public void onPageFinished(WebView view, String url) {
                    if (progressBar.isShowing()) {
                        progressBar.dismiss();
                    }
                }

                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    if (progressBar.isShowing()) {
                        progressBar.dismiss();
                    }
                }
            });


            //To handle Webpage back in fragment
            webView.setOnKeyListener(new View.OnKeyListener() {

                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK
                            && event.getAction() == MotionEvent.ACTION_UP
                            && webView.canGoBack()) {
                        webView.goBack();
                        return true;
                    }
                    return false;
                }
            });


            return view;
        }

        @Override
        public void onDestroyView() {
            super.onDestroyView();
            unbinder.unbind();
        }
    }


activity_dashboard.xml


    <?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout 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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.mplussoft.android.jobscruze.Activity.DashboardActivity">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />

        <!-- The main content view -->
        <RelativeLayout
            android:id="@+id/mainContent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

    <!--

            <WebView
                android:id="@+id/webView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    -->


        </RelativeLayout>


    </LinearLayout>

    <!-- The navigation drawer -->
    <LinearLayout
        android:id="@+id/drawerPane"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/white"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <!-- Profile Box -->


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:background="@drawable/sidebar_bg"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/profile_image"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_margin="10dp"
                android:background="@mipmap/ic_launcher_round"
                android:padding="2dp"
                app:civ_border_color="@color/white"
                app:civ_border_width="2dp" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/txt_userName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="Johnson Doe"
                    android:textColor="@color/black"
                    android:textSize="@dimen/txtsize_normal"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/txt_city"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="4dp"
                    android:text="Pune"
                    android:textSize="@dimen/txtsize_normal" />


            </LinearLayout>
        </LinearLayout>

        <!-- List of Actions (pages) -->
        <ListView
            android:id="@+id/navList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="30dp"
            android:scrollbars="none"
            android:background="#ffffffff"
            android:choiceMode="singleChoice"
            android:divider="@color/colorSidemenudivider"
            android:dividerHeight="1dp" />

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




drawer_item.xml


    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="30dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:layout_marginRight="30dp"
    android:layout_marginTop="30dp">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="25dp"
        android:layout_height="30dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="5dp"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/icon"
        android:gravity="center_vertical"
        android:textColor="@color/colorSidemenuText"
        android:textSize="@dimen/txtsize_normal" />

    <TextView
        android:id="@+id/subTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/title"
        android:layout_toRightOf="@+id/icon"
        android:visibility="gone" />

</RelativeLayout>


toolbar.xml


    <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/tool"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@color/colortoolbar"
       >

        <ImageView
            android:id="@+id/toggle"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="20dp"
            android:layout_centerVertical="true"
            android:scaleType="centerInside"
            android:src="@drawable/toggle" />

        <ImageView
            android:id="@+id/img_logo"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:scaleType="centerInside"
            android:src="@drawable/jobs_white" />

    </RelativeLayout>


</RelativeLayout>