在点击列表项时,片段中的AbsListView.OnItemClickListener的实现不会执行任何操作,也不会显示错误。
我阅读了文档,搜索了Google& SOF并阅读SDK源代码,但无法解决问题。
public class VenueActivity extends Activity implements
NBVenuesFragment.OnFragmentInteractionListener,
NBFriendsFragment.OnFragmentInteractionListener,
UploadedPhotosFragment.OnFragmentInteractionListener {
private static final String TAB_KEY_INDEX = "tab_key";
private static final String TAG = "VenueActivity";
private Fragment mCurrentFragment = null;
private Fragment mNewFragment = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_venue);
ActionBar actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab mVenuesFragmentTab = actionbar.newTab().setText(
getString(R.string.ui_nearby_venues_tab));
ActionBar.Tab mNBFriendsFragmentTab = actionbar.newTab().setText(
getString(R.string.ui_nearby_friends_tab));
ActionBar.Tab mUploadedPhotosFragmentTab = actionbar.newTab().setText("Photos Uploaded");
Fragment mVenuesFragment = new NBVenuesFragment();
Fragment mNBFriendsFragment = new NBFriendsFragment();
Fragment mUploadedPhotosFragment = new UploadedPhotosFragment();
mVenuesFragmentTab.setTabListener(new VenueTabsListener(mVenuesFragment,
getApplicationContext()));
mNBFriendsFragmentTab.setTabListener(new VenueTabsListener(mNBFriendsFragment,
getApplicationContext()));
mUploadedPhotosFragmentTab.setTabListener(new VenueTabsListener(mUploadedPhotosFragment,
getApplicationContext()));
actionbar.addTab(mVenuesFragmentTab);
//actionbar.addTab(mNBFriendsFragmentTab);
actionbar.addTab(mUploadedPhotosFragmentTab);
if (savedInstanceState != null) {
actionbar.setSelectedNavigationItem(savedInstanceState.getInt(
TAB_KEY_INDEX, 0));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.venue_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_check_in:
Toast.makeText(this, getString(R.string.action_check_in),
Toast.LENGTH_SHORT).show();
return true;
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.onBackPressed();
return true;
}
return false;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(TAB_KEY_INDEX, getActionBar()
.getSelectedNavigationIndex());
}
@Override
public void onNearByVenueSelection(VenuesDummyData.VenuesDummyItem venueData){
Bundle args = new Bundle();
args.putString("VENUE_ID", venueData.venueId);
args.putString("VENUE_NAME", venueData.venueName);
args.putString("VENUE_ADDRESS", venueData.venueAddress);
args.putString("VENUE_CHECK_INS", venueData.venueCheckIns);
args.putString("VENUE_LIKES", venueData.venueLikes);
args.putString("VENUE_CHECK_INS_FLIRT", venueData.venueCheckInsflirt);
args.putString("VENUE_CHECK_INS_FRIENDS", venueData.venueCheckInsFriends);
args.putString("OMEGA", venueData.omega);
Fragment mVenueDetailsFragment = new VenueDetailsFragment();
mVenueDetailsFragment.setArguments(args);
mCurrentFragment = new NBVenuesFragment();
mNewFragment = mVenueDetailsFragment;
FragmentManager mFragmentManager = getFragmentManager();
String tag = mVenueDetailsFragment.getTag();
FragmentTransaction ft = mFragmentManager.beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.replace(R.id.fragment_container, mVenueDetailsFragment, tag);
ft.commit();
}
@Override
public void onNearByFriendSelection(JsonObject mUserInfo){
Bundle args = new Bundle();
args.putString("FRIEND_NAME", mUserInfo.get("name").getAsString());
//args.putString("FRIEND_ID", mUserInfo.friendId);
args.putString("FRIEND_PHOTO", mUserInfo.get("photo_url").getAsString());
//args.putString("VENUE_ID", mUserInfo.venueId);
//args.putString("VENUE_NAME", mUserInfo.venueName);
Fragment mFriendDetailsFragment = new FriendDetailsFragment();
mFriendDetailsFragment.setArguments(args);
mCurrentFragment = new NBFriendsFragment();
mNewFragment = mFriendDetailsFragment;
FragmentManager mFragmentManager = getFragmentManager();
String tag = mFriendDetailsFragment.getTag();
FragmentTransaction ft = mFragmentManager.beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.replace(R.id.fragment_container, mFriendDetailsFragment, tag);
//ft.addToBackStack(tag);
ft.commit();
}
@Override
public void onUserSelection(JsonObject mUserInfo){
Log.i(TAG, "onUserSelection");
Log.i(TAG, "mUserInfo ==> " + mUserInfo.toString());
Bundle args = new Bundle();
args.putString("USER_NAME", mUserInfo.get("name").getAsString());
//args.putString("FRIEND_ID", mUserInfo.friendId);
args.putString("USER_PHOTO", mUserInfo.get("photo_url").getAsString());
//args.putString("VENUE_ID", mUserInfo.venueId);
//args.putString("VENUE_NAME", mUserInfo.venueName);
Fragment mUserDetailsFragment = new UserDetailsFragment();
mUserDetailsFragment.setArguments(args);
mCurrentFragment = new UploadedPhotosFragment();
mNewFragment = mUserDetailsFragment;
FragmentManager mFragmentManager = getFragmentManager();
String tag = mUserDetailsFragment.getTag();
FragmentTransaction ft = mFragmentManager.beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.replace(R.id.fragment_container, mUserDetailsFragment, tag);
//ft.addToBackStack(tag);
ft.commit();
}
@Override
public void onBackPressed() {
if (mCurrentFragment != null && mNewFragment != null) {
String tag = mCurrentFragment.getTag();
FragmentManager mFragmentManager = getFragmentManager();
FragmentTransaction ft = mFragmentManager.beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.remove(mNewFragment);
ft.replace(R.id.fragment_container, mCurrentFragment, tag);
ft.commit();
mCurrentFragment = null;
mNewFragment = null;
} else {
super.onBackPressed();
}
}
}
class VenueTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public Context context;
public VenueTabsListener(Fragment fragment, Context context) {
this.fragment = fragment;
this.context = context;
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
//Toast.makeText(context, "Reselected!", Toast.LENGTH_SHORT).show();
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
//Toast.makeText(context, "Selected!", Toast.LENGTH_SHORT).show();
ft.replace(R.id.fragment_container, fragment);
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
//Toast.makeText(context, "Unselected!", Toast.LENGTH_SHORT).show();
ft.remove(fragment);
}
}
public class UploadedPhotosFragment extends Fragment implements AbsListView.OnItemClickListener {
private OnFragmentInteractionListener mListener;
private AbsListView mListView;
private JsonObject mUserInfo;
private PhotosAdapter mAdapter;
private JsonArray mDataFromServer;
private String TAG = "UploadedPhotosFragment";
private void getPhotosData() {
Log.i(TAG, "getPhotosData");
AsyncTask<Void, Void, FlefuApiMessagesGetPhotosResponseMessage> photosJsonString;
photosJsonString = new GetPhotos() {
@Override
protected void onPostExecute(FlefuApiMessagesGetPhotosResponseMessage response) {
Log.i(TAG, "@overridden GetPhotos onPostExecute: " + response.getData());
mDataFromServer = (JsonArray)new JsonParser().parse(response.getData());
mUserInfo = new JsonObject();
for (int i = 0; i < mDataFromServer.size(); i++) {
mUserInfo = mDataFromServer.get(i).getAsJsonObject();
mAdapter.add(mUserInfo);
}
}
};
photosJsonString.execute();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPhotosData();
mAdapter = new PhotosAdapter(getActivity());
Log.i(TAG, "onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(TAG, "onCreateView");
View view = inflater.inflate(R.layout.fragment_photo_image, container, false);
//int cols = getResources().getDisplayMetrics().widthPixels / getResources().getDisplayMetrics().densityDpi;
mListView = (AbsListView) view.findViewById(R.id.photos);
//mListView.setNumColumns(3);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(this);
return view;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
private class PhotosAdapter extends ArrayAdapter <JsonObject> {
private LayoutInflater inflater;
private class ViewHolder {
TextView name;
//TextView venue;
ImageView thumb;
}
public PhotosAdapter(Context context) {
super(context, R.layout.uploaded_image);
Log.i(TAG, context.toString());
inflater = LayoutInflater.from(context);
Log.i(TAG, "PhotosAdapter");
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.i(TAG, "getView");
ViewHolder holder;
if(convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.uploaded_image, null);
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.thumb = (ImageView) convertView.findViewById(R.id.image);
//holder.venueName = (TextView) convertView.findViewById(R.id.venueName);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// select the image view
Ion.with(holder.thumb)
.resize(300, 300)
.centerCrop()
//.placeholder(R.drawable.placeholder)
//.error(R.drawable.error)
.load(getItem(position).get("thumb_url").getAsString());
holder.name.setText(getItem(position).get("name").getAsString());
return convertView;
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i(TAG, "parent ==> " + parent.toString());
Log.i(TAG, "view ==> " + view.toString());
if (mListener != null) {
Log.i(TAG, "call to onUserSelection ==> " +
mDataFromServer.get(position).getAsJsonObject().toString());
mListener.onUserSelection(mDataFromServer.get(position).getAsJsonObject());
}
}
public interface OnFragmentInteractionListener {
public void onUserSelection(JsonObject mUserInfo);
}
}
答案 0 :(得分:1)
您没有初始化您的mListener
值。您的mListener为null,单击ListView
项时没有任何反应。