gpt4 book ai didi

java - 如何在 fragment 中使用ListView和onClickListener?

转载 作者:太空宇宙 更新时间:2023-11-04 13:03:18 25 4
gpt4 key购买 nike

如何将 listview 和 onclicklistener 添加到我的 tab1fragment.xml 布局中?

我有多个选项卡,但如果我有一个例子,我想我可以解决其余的问题..我已经尝试过其他答案,但到目前为止还没有成功。

这是我的作品..谢谢大家

MyTrans.java

package com.storey.user.storeytom.fragments;

import java.util.List;
import java.util.Vector;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabContentFactory;

import com.storey.user.storeytom.adapters.MyFragmentPagerAdapter;
import com.storey.user.storeytom.R;

public class MyTrans extends Fragment implements OnTabChangeListener,
OnPageChangeListener {

private TabHost tabHost;
private ViewPager viewPager;
private MyFragmentPagerAdapter myViewPagerAdapter;
int i = 0;
View v;

@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

v = inflater.inflate(R.layout.tabs_viewpager_layout, container, false);



i++;

// init tabhost
this.initializeTabHost(savedInstanceState);

// init ViewPager
this.initializeViewPager();


return v;
}

// fake content for tabhost
class FakeContent implements TabContentFactory {
private final Context mContext;

public FakeContent(Context context) {
mContext = context;
}

@Override
public View createTabContent(String tag) {
View v = new View(mContext);
v.setMinimumHeight(0);
v.setMinimumWidth(0);
return v;
}
}

private void initializeViewPager() {
List<Fragment> fragments = new Vector<Fragment>();

fragments.add(new Tab1Fragment());
fragments.add(new Tab2Fragment());
fragments.add(new Tab3Fragment());

this.myViewPagerAdapter = new MyFragmentPagerAdapter(
getChildFragmentManager(), fragments);
this.viewPager = (ViewPager) v.findViewById(R.id.viewPager);
this.viewPager.setAdapter(this.myViewPagerAdapter);
this.viewPager.setOnPageChangeListener(this);

}

private void initializeTabHost(Bundle args) {

tabHost = (TabHost) v.findViewById(android.R.id.tabhost);
tabHost.setup();

for (int i = 1; i <= 3; i++) {

TabHost.TabSpec tabSpec;
tabSpec = tabHost.newTabSpec("Tab " + i);
tabSpec.setIndicator("Tab " + i);
tabSpec.setContent(new FakeContent(getActivity()));
tabHost.addTab(tabSpec);
}
tabHost.setOnTabChangedListener(this);
}

@Override
public void onTabChanged(String tabId) {
int pos = this.tabHost.getCurrentTab();
this.viewPager.setCurrentItem(pos);

HorizontalScrollView hScrollView = (HorizontalScrollView) v
.findViewById(R.id.hScrollView);
View tabView = tabHost.getCurrentTabView();
int scrollPos = tabView.getLeft()
- (hScrollView.getWidth() - tabView.getWidth()) / 2;
hScrollView.smoothScrollTo(scrollPos, 0);

}

@Override
public void onPageScrollStateChanged(int arg0) {
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}

@Override
public void onPageSelected(int position) {
this.tabHost.setCurrentTab(position);
}
}

Tab1Fragment.java

    package com.storey.user.storeytom.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.storey.user.storeytom.R;
public class Tab1Fragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View v = inflater.inflate(R.layout.tab1fragment, container, false);
return v;
}
}

tab1fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Fragment 1" />

</LinearLayout>

tabs_viewpager_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<HorizontalScrollView
android:id="@+id/hScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >

<TabWidget
android:id="@android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TabWidget>
</HorizontalScrollView>

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
</TabHost>

</LinearLayout>

最佳答案

您可以在 TextView 下面添加一个 ListView,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Fragment 1" />


<ListView
android:id="+@id/myList1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

对于列表,您不需要一个 onClickListener ,它只会对列表上的点击使用react(不用介意具体在哪里)。您需要一个 onItemClickListener,您可以在 FragmentonCreateView() 方法中设置它:

ListView lvList1 = (ListView) v.findViewById(R.id.myList1);
lvList1.setOnItemClickListener(new OnItemClickListener{... });

有关 onItemClickListener 的教程,请参阅 this link .

关于java - 如何在 fragment 中使用ListView和onClickListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34727710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com