我無法弄清楚我做錯了什麼。我有一個導航抽屜的設置,當我點擊導航抽屜中的其中一個項目時,它會加載我使用適配器自定義的列表視圖的一個片段。那部分工作。onclicklistener不工作從導航抽屜裏的片段
但我在該片段內添加了一個onclicklistener,點擊時我想加載一個新片段。但是當我點擊列表視圖中的項目時,什麼都不會發生。 這是我的代碼。
這是EventsFragment.java,這是加載自定義列表視圖。
package org.nctta.nctta_tournaments.fragments;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.AdapterView;
import org.nctta.nctta_tournaments.R;
import org.nctta.nctta_tournaments.Tournament;
import org.nctta.nctta_tournaments.TournamentAdapter;
import java.util.ArrayList;
/**
* Created by John on 9/10/2017.
*/
public class EventsFragment extends Fragment {
public EventsFragment() {
//Required empty public constructor
}
View myView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.events_layout, container,false);
//create a list of tournaments for testing
final ArrayList<Tournament> Tournaments = new ArrayList<Tournament>();
Tournaments.add(new Tournament("1","Ohio East Fall Tournament","1/1/2017", "1/1/2017","University of Akron"));
Tournaments.add(new Tournament("2","Ohio West Fall Tournament","1/1/2017", "1/1/2017","Ohio State University"));
Tournaments.add(new Tournament("3","Upper Midwest Fall Tournament","1/1/2017", "1/1/2017","University of Iowa"));
Tournaments.add(new Tournament("4","Central Plains Fall Tournament","1/1/2017", "1/1/2017","Univeristy of Centeral Plains"));
Tournaments.add(new Tournament("5","Lower Midwest Fall Tournament","1/1/2017", "1/1/2017","Lindenwood University"));
Tournaments.add(new Tournament("6","Midwest Regional Tournament","1/1/2017", "1/1/2017","Lindenwood University"));
Tournaments.add(new Tournament("7","Great Lakes Regional Tournament","1/1/2017", "1/1/2017","Lindenwood University"));
Tournaments.add(new Tournament("8","South Regional Tournament","1/1/2017", "1/1/2017","Lindenwood University"));
//Create an Tournament Adapter
TournamentAdapter adapter = new TournamentAdapter(getActivity(),Tournaments);
//find the listview object in the view hierarchy
ListView listView = (ListView) myView.findViewById(R.id.tourList);
listView.setAdapter(adapter);
//set a click listener to open tournament info page
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l){
//get the tournament object at the given position the user clicked on
Tournament tournament = Tournaments.get(position);
//start tournamentactivity
android.app.FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new TournamentFragment())
.commit();
}
});
return myView;
}
}
這裏是我的TournamentFragment,當一個項目被點擊時應該加載。這個片段我想加載頁面瀏覽器與標籤,我無法去工作,所以現在我評論了這些東西。只是想讓clicklistener先工作。
package org.nctta.nctta_tournaments.fragments;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import org.nctta.nctta_tournaments.R;
/**
* Created by John on 9/15/2017.
*/
public class TournamentFragment extends Fragment {
View myView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.tournament_tabs, container,false);
/*
//Find the view pager that will allow the user to swipe between fragments
ViewPager viewPager = (ViewPager) myView.findViewById(R.id.viewpager);
//create an adapter that knows which fragment should be shown on each page
TournTabsAdapter adapter = new TournTabsAdapter(this, getChildFragmentManager());
//set the adapter onto the view pager
viewPager.setAdapter(adapter);
//find the tab layout that shows the tabs
TabLayout tabLayout = (TabLayout) myView.findViewById(R.id.tabs);
// Connect the tab layout with the view pager. This will
// 1. Update the tab layout when the view pager is swiped
// 2. Update the view pager when a tab is selected
// 3. Set the tab layout's tab names with the view pager's adapter's titles
// by calling onPageTitle()
tabLayout.setupWithViewPager(viewPager);
*/
return myView;
}
}
任何想法?
這是onItemClickListener調用嗎?把一個日誌調用在那裏,看看它是否被稱爲 – joao86
我通過在那裏的吐司消息,當我調試它永遠不會被觸發 – JED
因此,顯然它是聽衆不工作。那麼嘗試添加onclick事件到你的適配器 – joao86