在片段内部ONCLICK LISTVIEW并调用另一个片段

时间:2017-09-07 17:32:22

标签: java android android-fragments

我是Android工作室的新手。我正在创建一个应用程序,其中我正在使用导航抽屉活动。

也许在我的抽屉里有HTML,PHP,JAVASCRIPT。 当我点击HTML时,有一个片段显示HTML中不同主题的列表。

所以我的问题是,如何在片段中的listview中分配onclick以显示另一个片段?

这是我的主要activity.java文件:

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

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

    if (id == R.id.nav_html) {
        FragmentHome fragmentHome = new FragmentHome();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.mainLayout, fragmentHome)
                .commit();

    } else if (id == R.id.nav_js) {

    } else if (id == R.id.nav_php) {

    } else if (id == R.id.nav_compiler) {

    }

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

这是我的FRAGMENTHOME((也称为HTML片段))

package com.webdevkotoreal.coleng.webdevttoolz;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;


        /**
         * A simple {@link Fragment} subclass.
         */
public class FragmentHome extends Fragment {

    public FragmentHome() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_fragment_home, container, false);
        String [] menuHtml = {"Introduction", "HTML Basics", "HTML Comments" };
        ListView listView = (ListView) view.findViewById(R.id.htmlList);

        ArrayAdapter<String> htmlListAdapter = new ArrayAdapter<String>(
            getActivity(),
            android.R.layout.simple_list_item_1,
            menuHtml
        );
        listView.setAdapter(htmlListAdapter);

    // Inflate the layout for this fragment
        return view;
    }

}

我上网但似乎没有适合我的问题的答案。 TIA!

0 个答案:

没有答案