onListItemClick在使用选项卡导航时从未调用过

时间:2015-07-16 05:22:43

标签: android android-listview

我有一个ListActivity,它显示了一个体育俱乐部排名球员的阶梯。导航模式设置为选项卡。当我尝试实现onListItemClick时,它永远不会被调用。我仔细阅读了很多其他帖子,例如Android: onListItemClick not getting called in ListActivityonListItemClick is not getting called on ListActivity,其中人们建议将列表中的小部件设置为android:focusable="false",但这对我来说似乎没有用。我不知道标签导航是否与此有关,但无论如何 - 下面是我的代码。

活动onCreate to onListItemClick

public class Rankings extends ListActivity
{

    ArrayList<Club> clubs;
    public static ArrayList<RankedPlayer> Women = new ArrayList<RankedPlayer>();
    public static ArrayList<RankedPlayer> Men = new ArrayList<RankedPlayer>();
    int menuPosition = 0;
    private static Context context;
    public static TabSelected _tab;
    RankingEditorMode mode;


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

        clubs = getIntent().getParcelableArrayListExtra("clubs");

        context = Rankings.this;

        final ActionBar actionbar = getActionBar();
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        Tab men = actionbar.newTab();
        men.setText("Men");
        men.setIcon(R.drawable.signs_man_32x32);
        men.setTabListener(new TabListener() 
        {

            @Override
            public void onTabUnselected(Tab tab, FragmentTransaction ft) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTabSelected(Tab tab, FragmentTransaction ft) 
            {
                _tab = TabSelected.Men;
                GoToFragment();
            }

            @Override
            public void onTabReselected(Tab tab, FragmentTransaction ft) {
                // TODO Auto-generated method stub

            }
        });

        Tab women = actionbar.newTab();
        women.setText("Women");
        women.setIcon(R.drawable.signs_woman_32x32);
        women.setTabListener(new TabListener() 
        {

            @Override
            public void onTabUnselected(Tab tab, FragmentTransaction ft) 
            {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTabSelected(Tab tab, FragmentTransaction ft) 
            {
                _tab = TabSelected.Women;
                GoToFragment();
            }

            @Override
            public void onTabReselected(Tab tab, FragmentTransaction ft) 
            {
                // TODO Auto-generated method stub

            }
        });

        actionbar.addTab(men);
        actionbar.addTab(women);


        GetRankings();
    }

    @Override
    protected void onListItemClick(ListView list, View view, int position, long id)
    {
        super.onListItemClick(list, view, position, id);

        System.out.println("\n\n\n>>>>>>>>>>   Popup <<<<<<<<<<<<<<<<<\n\n\n");

        AlertDialog.Builder popup = new AlertDialog.Builder(Rankings.this);
        popup.setCancelable(true);
        popup.setTitle("Share Log?");

        String message = clubs.get(menuPosition).getName();
        if(_tab == TabSelected.Men)
        {
            message += "\t-\t Men";
        }
        else
        {
            message += "\t-\t Women";
        }
        popup.setMessage(message);

        popup.setPositiveButton("Share", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                ShareLog();
            }
        });

        popup.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which) 
            {
            }
        });
        popup.show();
    }

片段

public class ranking_fragment extends ListFragment
{
    @Override
    public void onActivityCreated(Bundle bundle)
    {
        super.onActivityCreated(bundle);
        ArrayList<RankedPlayer> rankings;
        if(Rankings._tab == TabSelected.Men)
        {
            rankings = Rankings.Men;
        }
        else
        {
            rankings = Rankings.Women;
        }

        RankingAdapter adapter = new RankingAdapter(getActivity(), rankings);
        setListAdapter(adapter);
    }

}

适配器

public class RankingAdapter extends ArrayAdapter<RankedPlayer>
{

      private final Context context;
      ArrayList<RankedPlayer> rankings;

    public RankingAdapter(Context context, ArrayList<RankedPlayer> rankings) 
    {
        super(context, R.layout.jarvis_log, rankings);
        this.context = context;
        this.rankings = rankings;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.ranking_layout, parent, false);

        TextView rank = (TextView)rowView.findViewById(R.id.tvRanking_rank);
        TextView name = (TextView)rowView.findViewById(R.id.tvRanking_name);
        TextView club = (TextView)rowView.findViewById(R.id.tvRanking_club);
        TextView points = (TextView)rowView.findViewById(R.id.tvRanking_points);

        rank.setText((position+1)+".");
        name.setText("\t"+rankings.get(position).getName());
        points.setText(rankings.get(position).getPoints()+"\t");

        if(rankings.get(position).getClub().length() >= 1)
        {
            club.setText("\t("+rankings.get(position).getClub()+")");
        }
        else
        {
            club.setText("");
        }
        return rowView;
    }

}

活动的布局xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context="rankings.Rankings"
    tools:ignore="MergeRootFrame" >
<ListView 
    android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</FrameLayout>

ranking_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/OrangeRed"
    android:clipToPadding="false"
    android:orientation="vertical"
    android:paddingBottom="4dp"
    android:scrollbarAlwaysDrawVerticalTrack="false"
    android:descendantFocusability="blocksDescendants"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:clickable="false">

    <TextView
        android:id="@+id/tvRanking_rank"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="1."
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/DarkBlue" 
        android:focusable="false"
        android:clickable="false"
        android:focusableInTouchMode="false"/>

    <TextView
        android:id="@+id/tvRanking_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/tvRanking_rank"
        android:text="Steve Coppinger"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/DarkBlue"
        android:textStyle="bold" 
        android:focusable="false"
        android:clickable="false"
        android:focusableInTouchMode="false"/>

    <TextView
        android:id="@+id/tvRanking_points"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="900"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/DarkBlue"
        android:focusable="false"
        android:clickable="false" 
        android:focusableInTouchMode="false"/>

    <TextView
        android:id="@+id/tvRanking_club"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/tvRanking_name"
        android:text=" (KZN)"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/DarkBlue"
        android:focusable="false"
        android:clickable="false" 
        android:focusableInTouchMode="false"/>

</RelativeLayout>

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:1)

您的实际列表不在活动中,而是在片段中。因此永远不会调用onListItemClick中的Activity。您必须在Fragment类中实现它。您的Activity也不一定是ListActivity

答案 1 :(得分:0)

您的主要活动中没有片段声明,这就是为什么它甚至不能被活动看到。您只需创建一个活动,listview必须在主活动中加载,只需通过片段调用适配器。