单击ListViewItem时Android加载片段
我试图在列表视图中单击listview项时加载一个片段。 我在滑动选项卡布局中有几个选项卡,每个选项卡都包含一个包含多个项目的列表视图。 当选项卡被更改时,我将对列表中的第一项进行剪辑,以便加载带有控件的相应片段 现在,当我单击列表视图时,我正在努力加载第一个片段 有谁能帮我做到这一点吗 fragment_manual_mode.xml单击ListViewItem时Android加载片段,android,android-fragments,android-listview,Android,Android Fragments,Android Listview,我试图在列表视图中单击listview项时加载一个片段。 我在滑动选项卡布局中有几个选项卡,每个选项卡都包含一个包含多个项目的列表视图。 当选项卡被更改时,我将对列表中的第一项进行剪辑,以便加载带有控件的相应片段 现在,当我单击列表视图时,我正在努力加载第一个片段 有谁能帮我做到这一点吗 fragment_manual_mode.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="h
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/fragment_manual"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="@android:color/holo_orange_light">
<Button
android:id="@+id/f1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="F1" />
<Button
android:id="@+id/f2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/f1"
android:text="F2" />
<Button
android:id="@+id/s1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:text="S1" />
<Button
android:id="@+id/s2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_below="@+id/s1"
android:text="S2" />
<TextView
android:id="@+id/text_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/s2"
android:layout_centerHorizontal="true"
android:layout_gravity="left|bottom"
android:text="New Text" />
</RelativeLayout>
活动_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:id="@+id/main_activity"
>
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<com.grozeaion.www.gvicameraremotecontrol.SlidingTabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:background="@color/ColorPrimary"
android:layout_below="@+id/tool_bar"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_height="match_parent"
android:layout_width="300dp"
android:layout_weight="1"
android:background="#bde354"
android:layout_below="@+id/tabs">
</android.support.v4.view.ViewPager>
<fragment
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:name="com.grozeaion.www.gvicameraremotecontrol.dummy"
android:id="@+id/dummy"
tools:layout="@layout/fragment_dummy"
android:layout_alignTop="@+id/pager"
android:layout_toRightOf="@+id/pager"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
</RelativeLayout >
您在哪里向ListView注册了侦听器 改为
...extends ListFragment implements OnItemClickListener //class declaration
getListView().setOnItemClickListener(this) // in onviewcreated/onActivityCreated this is the implemented OnItemClickListener
@Override // add this method in the Tab Modes
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//your code when ListItem is clicked
}
使用
Fragment
中的newInstance
模式(更好的做法),请参见此处-感谢您的帮助。我已通过使用我已经按照你的建议用框架布局替换虚拟片段
<FrameLayout
android:id="@+id/frame_modes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tabs"
android:layout_toEndOf="@+id/pager">
</FrameLayout>
我还使用了lv项单击上的片段更改
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FragmentTransaction ft= getFragmentManager().beginTransaction();
switch (position) {
case 0:
ft.replace(R.id.frame_modes, ModeManual.newInstance("Manual"));
break;
case 1:
ft.replace(R.id.frame_modes, ModeTriggered.newInstance("Triggered"));
break;
case 2:
ft.replace(R.id.frame_modes, ModeBullet.newInstance("Bullet"));
break;
case 3:
ft.replace(R.id.frame_modes, ModeManualPan.newInstance("Manual Pan"));
break;
case 4:
ft.replace(R.id.frame_modes, ModeUSB1.newInstance("USB 1"));
break;
}
ft.commit();
}
@覆盖
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
FragmentTransaction ft=getFragmentManager().beginTransaction();
开关(位置){
案例0:
ft.replace(R.id.frame_模式,ModeManual.newInstance(“手动”));
打破
案例1:
ft.replace(R.id.frame_模式,ModeTriggered.newInstance(“Triggered”);
打破
案例2:
ft.replace(R.id.frame_模式,ModeBullet.newInstance(“Bullet”));
打破
案例3:
ft.replace(R.id.frame_模式,ModeManualPan.newInstance(“手动Pan”));
打破
案例4:
ft.replace(R.id.frame_模式,ModeUSB1.newInstance(“USB 1”));
打破
}
ft.commit();
}
添加其他片段
再次感谢你们的帮助,到目前为止,我对Android的学习曲线很陡峭。
但是我现在就要开始艰苦的工作了,我需要在I2C、SPI和串行接口上连接一些传感器,以使应用程序按预期工作。您遇到了什么错误?您好,我已经按照您的建议添加了列表视图的侦听器,但第二个片段仍然没有加载到接口中。关于片段的生成方式,我仍在研究。@ION GROZEA听众是否了解它?你能在R.id.pager所在的位置添加布局吗?嗨,在我的应用程序中,主要活动有一个SlidengTableOut,每个选项卡都包含一个包含不同项目的列表视图,基本上,每次用户滑动到新选项卡时,viewpager都会加载一个新片段-请参见选项卡模式。我想添加/替换一个片段,该片段包含多个控件,每当用户单击列表视图项的某个时,该片段必须显示在列表视图的左侧。在这里我完全迷路了,我不知道如何添加这个片段查看postHi末尾的图片,我找到了如下解决方案:我在主活动中创建了一个虚拟片段,并为每个firs列表视图项创建了几个片段。当用户滑动主要类别时,我试图在屏幕上加载第一个片段。这在一定程度上是有效的,因为在运行时我知道必须替换虚拟片段,但我不知道在几个滚动之后如何进行替换。另一个问题是,在第一次运行页面适配器时,调用两次tabs模式,基本上我总是得到与第二个lv项0对应的片段,而不是第一个lv项。你知道怎么解决这个问题吗?@IONGROZEA我不知道你到底面临什么问题。但是1。您不需要使用框架布局或其他布局2。要在ViewPager中检测滑动页面的更改时间,请使用-(OnPageChangeListener)3。我不理解这个部分-parent.getItemAtPosition(0).toString()。为什么?我想你想使用-items.get(position)。
...extends ListFragment implements OnItemClickListener //class declaration
getListView().setOnItemClickListener(this) // in onviewcreated/onActivityCreated this is the implemented OnItemClickListener
@Override // add this method in the Tab Modes
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//your code when ListItem is clicked
}
myControls = new ModeControls();
myControls.setArguments(arguments);
<FrameLayout
android:id="@+id/frame_modes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tabs"
android:layout_toEndOf="@+id/pager">
</FrameLayout>
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
SlidingTabLayout slidingTabLayout;
CharSequence Titles[] = {"Basic", "Sensors", "Fast Objects", "Pan Tilt", "USB"};
int Numboftabs = 5;
FragmentTransaction ft;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creating The Toolbar and setting it as the Toolbar for the activity
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles for the Tabs and Number Of Tabs.
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles);
// Assigning ViewPager View and setting the adapter
viewPager = (ViewPager) findViewById(R.id.pager);
ft = getSupportFragmentManager().beginTransaction();
if (savedInstanceState == null) {
ft.replace(R.id.frame_modes, ModeManual.newInstance("Manual"));
ft.commit();
}
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {
}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
public void onPageSelected(int position) {
ft = getSupportFragmentManager().beginTransaction();
switch (position) {
case 0:
ft.replace(R.id.frame_modes, ModeManual.newInstance("Manual"));
break;
case 1:
ft.replace(R.id.frame_modes, ModeTriggered.newInstance("Triggered"));
break;
case 2:
ft.replace(R.id.frame_modes, ModeBullet.newInstance("Bullet"));
break;
case 3:
ft.replace(R.id.frame_modes, ModeManualPan.newInstance("Manual Pan"));
break;
case 4:
ft.replace(R.id.frame_modes, ModeUSB1.newInstance("USB 1"));
break;
}
ft.commit();
}
});
viewPager.setAdapter(viewPagerAdapter);
// Assiging the Sliding Tab Layout View
slidingTabLayout = (SlidingTabLayout) findViewById(R.id.tabs);
slidingTabLayout.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width
// Setting Custom Color for the Scroll bar indicator of the Tab View
slidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tabsScrollColor);
}
});
// Setting the ViewPager For the SlidingTabsLayout
slidingTabLayout.setViewPager(viewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FragmentTransaction ft= getFragmentManager().beginTransaction();
switch (position) {
case 0:
ft.replace(R.id.frame_modes, ModeManual.newInstance("Manual"));
break;
case 1:
ft.replace(R.id.frame_modes, ModeTriggered.newInstance("Triggered"));
break;
case 2:
ft.replace(R.id.frame_modes, ModeBullet.newInstance("Bullet"));
break;
case 3:
ft.replace(R.id.frame_modes, ModeManualPan.newInstance("Manual Pan"));
break;
case 4:
ft.replace(R.id.frame_modes, ModeUSB1.newInstance("USB 1"));
break;
}
ft.commit();
}