单击列表项中的按钮后如何移动到新片段

时间:2015-05-19 14:50:18

标签: android android-fragments android-listview

如何确保列表项中的按钮是可点击的,因为我试图使按钮可单击以便它可以转换为不同的片段?

与listview类似,其中的列表项是片段的一部分,我想确保单击列表项中的特定按钮将转换为新片段。

MainActivity代码: 它包含选项卡(并非所有选项卡都已实现,但现在不是问题)

public class MainActivity extends FragmentActivity implements HomeFragment.OnFragmentInteractionListener {



    private FragmentTabHost mTabHost;

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

        //set up the tabs which hold different fragments, it will currently crash because of the null
        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup(this,getSupportFragmentManager(), R.id.realtabcontent);
        mTabHost.addTab(mTabHost.newTabSpec("home").setIndicator("Home"), HomeFragment.class , null);
       // mTabHost.addTab(mTabHost.newTabSpec("search").setIndicator("Search"),null/*fragment here*/,null);
       // mTabHost.addTab(mTabHost.newTabSpec("post").setIndicator("Post"),null/*fragment here*/,null);
       // mTabHost.addTab(mTabHost.newTabSpec("books").setIndicator("Books"),null/*fragment here*/,null);
       // mTabHost.addTab(mTabHost.newTabSpec("me").setIndicator("Me"),null/*fragment here*/,null);
    }

    @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_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);
    }

    //For home fragment...parameter might change
    @Override
    public void onFragmentInteraction(String id) {

        Button viewPostings = (Button) findViewById(R.id.view_postings);
        viewPostings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PostDetailFragment fragment = PostDetailFragment.newInstance();
                getSupportFragmentManager().beginTransaction().replace(R.id.realtabcontent, fragment).addToBackStack(null).commit();
            }
        });
    }
}

我试图从onFragmentInteractionListener的实现中启动新活动。该应用程序加载列表视图及其内容,但其中的各个按钮不可点击。

3 个答案:

答案 0 :(得分:0)

使用某种ListAdapter(例如ArrayAdapter)填充ListView。这样,您就可以使用适配器来处理按钮点击。

答案 1 :(得分:0)

首先,您需要添加此行以使自定义列表项中的项目可单击:

android:descendantFocusability="blocksDescendants" 

将此属性添加到封闭布局。

其次,您需要在自定义ListView的自定义适配器的getView方法中设置onClickListener。例如,这将是这样的:

ImageButton deleteBtn = (ImageButton) view.findViewById(R.id.delete_btn);
deleteBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // code here
        }
    });

答案 2 :(得分:0)

在列表片段中,使用onItemClick:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    if (null != mListener) {
        // Notify the active callbacks interface (the activity, if the
        // fragment is attached to one) that an item has been selected.
        mListener.onFragmentInteraction(foodList.get(position));
    }
}

然后在您的父活动中,您将拥有类似的内容。

@Override
    public void onFragmentInteraction(Food food) {
        //going from ListFragment to DetailFragment
        //call detail fragment here
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        Fragment fragment = DetailFragment.newInstance(food);
        ft.replace(R.id.container, fragment);
        ft.addToBackStack( "tag" );
        ft.commit();
    }