2017-09-15 12 views
-2

무엇이 잘못되었는지 파악할 수 없습니다. 나는 네비게이션 서랍을 설정하고 탐색 서랍의 항목 중 하나를 클릭하면 어댑터로 사용자 정의한 목록보기의 조각을로드합니다. 그 부분은 작동합니다.onclicklistener가 내비게이션 드로어의 조각에서 작동하지 않습니다.

하지만 그 조각 내에서 onclicklistener를 추가하면 클릭하면 새 조각을로드하려고합니다. 그러나 listview에서 항목을 클릭해도 아무 일도 일어나지 않습니다. 여기 내 코드입니다.

이것은 EventsFragment.java입니다. 이것은 사용자 정의 listview를로드하는 것입니다.

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; 
} 

}

어떤 아이디어가?

+0

여기가 onItemClickListener입니까? 거기에 토스트 메시지를 통해 내가 – joao86

+0

라고 불리는 지 확인하기 위해 로그 콜을 둡니다. 결코 디버그되지 않습니다. – JED

+0

그래서 분명히 작동하지 않는 리스너입니다. 자, 어댑터에 onclick 이벤트를 추가해보십시오. – joao86

답변

0

괜찮습니까? 내 의견이 작동하지 않았는지 확실하지 않은 이유에 대해 의견을 말하고 도움을 청하면 ... 클릭 리스너의 항목이 작동하지 않는 주된 이유는 목록보기에 대한 사용자 정의 어댑터에는 imagebuttom이 포함되어 있습니다. 내가 그 일을 끝내면 리스너를 제거하고 어댑터 내부에 onclicklisteners를 추가합니다. 열쇠는 세트 태그를 사용하는 것이 었습니다.

여기 내 어댑터 코드입니다. 나는 확인을 위해 클릭 할 때 토스트 메시지를 건다.

package org.nctta.nctta_tournaments; 

/** 
* Created by John on 9/15/2017. 
*/ 

import android.app.Activity; 
import android.app.FragmentManager; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.content.Context; 
import android.widget.ImageButton; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.Toast; 

import org.nctta.nctta_tournaments.fragments.TournamentFragment; 

import java.util.ArrayList; 

public class TournamentAdapter extends ArrayAdapter<Tournament> { 

//create a new TournamentAdapter object. 
public TournamentAdapter(Context context, ArrayList<Tournament> tournaments) { 
    super(context, 0 , tournaments); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    //check if an existing view is being reuse, otherwise inflate the view 
    View listItemView = convertView; 
    if (listItemView ==null) { 
     listItemView = LayoutInflater.from(getContext()).inflate(
      R.layout.tournament_list_item, parent, false); 
    } 
    //get the current tournament object at this position in the list 
    Tournament currentTournament = getItem(position); 

    //Find the TExtView in the tournament_list_item.xml layout with iD of name 
    TextView TourName = (TextView) listItemView.findViewById(R.id.Name); 
    //get/set the tournament name of the current tournament object 
    TourName.setText(currentTournament.getTournamentName()); 
    //Find the TextView in the tournament_list_item.xml layout with ID of Location 
    TextView TourLocation = (TextView) listItemView.findViewById(R.id.Location); 
    //get/set the tournament location of the current tournament object; 
    TourLocation.setText(currentTournament.getTournamentLocation()); 
    //Find the linearlayout with id of text_container 
    LinearLayout textArea = (LinearLayout) listItemView.findViewById(R.id.text_container); 
    textArea.setTag(currentTournament.getTournamentName()); 
    //Find map view button with id of imageButton 
    ImageButton mapButton = (ImageButton) listItemView.findViewById(R.id.imageButton); 
    mapButton.setTag(currentTournament.getTournamentName()); 
    final Context context = parent.getContext(); 
    final FragmentManager fm = ((Activity) context).getFragmentManager(); 
    //set onclicklisteners 
    textArea.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String str = v.getTag().toString(); 
      Toast.makeText(getContext(),str + "TEXT", Toast.LENGTH_SHORT).show(); 
      //start tournament fragment 

      fm.beginTransaction() 
        .replace(R.id.content_frame, new TournamentFragment()) 
        .commit(); 
     } 
    }); 
    mapButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String str = v.getTag().toString(); 
      Toast.makeText(getContext(),str + "MAP", Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    return listItemView; 
} 
} 
+0

개선하기 위해 할 수있는 일은 콜백 인터페이스를 사용하는 것입니다. 어댑터 내부에서 조각 트랜잭션을 수행하지 않습니다. – joao86