我正在创建一个Android应用程序,我有一个带有android.support.design.widget.TabLayout和ViewPager的活动。
这是我的代码
activity_main.xml
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable" />
<android.support.v4.view.ViewPager
android:id="@+id/cards_grouping_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tabs" />
MainActivity.java
public class MainActivity extends AppCompatActivity {
@BindView(R.id.tabs)
TabLayout tabs;
@BindView(R.id.cards_grouping_pager)
ViewPager pager;
private ArrayList<Category> categories = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_cards);
ButterKnife.bind(this);
categories = //getDataFromApi
doSomeWorks();
}
private void doSomeWorks() {
MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager(), categories);
pager.setAdapter(adapter);
tabs.setupWithViewPager(pager);
LinearLayout tabStrip = (LinearLayout) tabs.getChildAt(0);
for (int i = 0; i < tabStrip.getChildCount(); i++) {
final int finalI = i;
tabStrip.getChildAt(i).setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//I SHOULD SHOW POPUP HERE
}
});
}
}
public class MyPagerAdapter extends FragmentPagerAdapter {
private final ArrayList<String> TITLES = new ArrayList<>();
MyPagerAdapter(FragmentManager fm, ArrayList<Category> categories) {
super(fm);
for (int i = 0; i < categories.size(); i++) {
TITLES.add(categories.get(i).name);
}
}
@Override
public CharSequence getPageTitle(int position) {
return TITLES.get(position);
}
@Override
public int getCount() {
return TITLES.size();
}
@Override
public Fragment getItem(int position) {
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
return HorizontalTabsAdapter.newInstance(position, categories, displayMetrics.widthPixels,
displayMetrics.heightPixels);
}
}
}
我想当用户长按每个标签时,弹出窗口恰好出现在所选位置 如下面的图片
我该怎么做?
答案 0 :(得分:0)
(没有足够的信用评论,所以张贴为答案)
要在tablayout的每个标签上实施长按clicklistener,请按照以下答案
https://stackoverflow.com/a/43522131/6387236
https://stackoverflow.com/a/34982710/6387236
要在长按位置实施弹出窗口,请按照这个不错的博客
进行操作https://rajeshandroiddeveloper.blogspot.in/2013/07/android-popupwindow-example-in-listview.html
希望这有帮助