我正在实现可滑动的标签。我有3个标签。在最后一个标签中有4个按钮,点击后会启动另一个片段活动。但是当我点击第二个标签中的可用空间时,同样的onclick就会被触发。请帮助我这是真的。
以下是我的补充项目链接https://drive.google.com/file/d/0B3znk-VNWY6RUHVvdDBpNWlmN3M
AlbumsFragment
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class AlbumsFragment extends Fragment implements OnClickListener {
View view;
Button albums,songs,artists,playlist;
Activity thisActivity;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
view=inflater.inflate(R.layout.fragment_albums, container,false);
thisActivity=getActivity();
albums=(Button) view.findViewById(R.id.btn_albums);
songs=(Button) view.findViewById(R.id.btn_allsongs);
artists=(Button) view.findViewById(R.id.btn_artists);
playlist=(Button) view.findViewById(R.id.btn_playlist);
albums.setOnClickListener(AlbumsFragment.this);
songs.setOnClickListener(AlbumsFragment.this);
artists.setOnClickListener(AlbumsFragment.this);
playlist.setOnClickListener(AlbumsFragment.this);
return view;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_albums:
Toast.makeText(thisActivity, "in", 100).show();
Intent i=new Intent(thisActivity,PlayList.class);
thisActivity.startActivity(i);
thisActivity.overridePendingTransition(R.anim.flip_left_to_right, R.anim.flip_right_to_left);
break;
default:
break;
}
}
}
这个onclicklistener在相邻的片段中被解雇了。
RecorderFragment
package com.krazzyplayer;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.SearchManager;
import android.content.ComponentName;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.SearchView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public class RecordFragment extends Fragment {
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view=inflater.inflate(R.layout.fragment_audio_recorder, container, false);
return view;
}
}
答案 0 :(得分:0)
为不可见的片段禁用onClick方法。为此,请添加此代码 -
if(!getUserVisibleHint())
{return false;}
在每个片段的第一行OnClick方法之前,如果片段不可见,则不会执行OnClick方法。