我的 Activity 中有一个表格内容,它有 3 个选项卡,每个选项卡内容都有一个 fragment ,并将它们命名为 OneFragment、TwoFragment 和 ThirdFragment
现在在处理我的第二个选项卡内容的 TwoFragment 类中,它有一个 ListView 控件。
现在的问题:
我不知道为什么列表控件不滚动!!
这是我的 Activity 代码:称之为 foot_recipe 类
package com.example.android.dezcook;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Foot_recipe extends AppCompatActivity {
/*Layout Properties define*/
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private Food send_info;
private int[] tabIcons = {
R.drawable.tab_details_imagealbum,
R.drawable.tab_details_cookie,
R.drawable.tab_details_camera
};
/*End layout propeties Definition*/
/*Start Logic definition from here*/
/*End Logic Definition*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_foot_recipe);
//get data from intent sended from cardview
String txtID = "";
Intent intent = getIntent();
// Bundle bundle=intent.getExtras();
// if(bundle!=null)txtID=bundle.getString("txtid");
Food food = (Food) intent.getParcelableExtra("food_obj");
send_info = food;
//tool bar customization
TextView toolbar_title = (TextView) findViewById(R.id.toolbar_title);
toolbar_title.setText(food.getTxtName());
toolbarCustomization();
//Tab customization
tabCustomization();
//Customize floatting button
ActionButton_fav(food.getTxtid());
}
public void ActionButton_fav(final int id) {
FloatingActionButton fav = (FloatingActionButton) findViewById(R.id.fav_btn_heart);
fav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (addDBfav(id) > 0)
Snackbar.make(v, "added to fav list", Snackbar.LENGTH_SHORT).show();
else
Snackbar.make(v, "not in fav list", Snackbar.LENGTH_SHORT).show();
}
});
}
public Food sendFrag_info() {
return send_info;
}
/*Layout Section
* All Layout code about food details are down side this page
* */
private void toolbarCustomization() {
toolbar = (Toolbar) findViewById(R.id.foodrestoolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
private void tabCustomization() {
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
tabLayout.getTabAt(2).select();
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "desc");
adapter.addFragment(new TwoFragment(), "material");
adapter.addFragment(new ThreeFragment(), "pic");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
public int addDBfav(int id) {
SQLiteDatabase sqLiteDatabase;
DbHelper myDbHelper = new DbHelper(this);
/* try{
myDbHelper.createDatabase();
}catch (IOException e)
{
throw new Error("unable to create database");
}*/
/* myDbHelper.openDataBase();
sqLiteDatabase = myDbHelper.getWritableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("UPDATE food SET fav=1 WHERE food_id ==" + id + ";", null);
Log.i("successfully:", Integer.toString(cursor.getCount()));
return 1;*/
myDbHelper.openDataBase();
ContentValues values=new ContentValues();
values.put("fav","1");
myDbHelper.openDataBase();
SQLiteDatabase sqLiteDatabase1=myDbHelper.getMyDataBase();
int part= sqLiteDatabase1.update("food",values,"food_id="+id,null);
myDbHelper.close();
Log.i("update","successful"+part);
return part;
}
}
这是我的 TwoFragment 类代码:
包 com.example.android.dezcook;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.example.android.dezcook.R;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
public class TwoFragment extends Fragment {
public TwoFragment()
{
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_two, container, false);
Foot_recipe foot_recipe=(Foot_recipe)getActivity();
Food food=foot_recipe.sendFrag_info();
/* ListView listView=(ListView)v.findViewById(R.id.show_items);
listView.setAdapter();
*/ String[] arr=food.getItems().split("،");
Log.i("LogArr",arr[0]);
// ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(),R.layout.list_items_frag, arr);
ListView listView = (ListView) v.findViewById(R.id.show_items);
listView.setAdapter(new details_list_adapter(getActivity(),arr));
return v;
}
}
和 TwoFragment xml 代码:
<LinearLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.android.dezcook.TwoFragment"
android:background="@drawable/gradient_background"
>
<!-- TODO: Update blank fragment layout -->
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/show_items"
android:divider="@color/colorPrimaryDark"
/>
</LinearLayout>
最后是调用foot_recipe xml代码的 Activity :
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:background="@drawable/xml_header_background"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/foodrestoolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@drawable/xml_header_background"
app:layout_scrollFlags="scroll|enterAlways"
android:layout_gravity="right"
app:title=" "
android:gravity="right"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="foot specail"
android:layout_gravity="right"
android:textSize="25dp"
android:textStyle="bold"
android:layout_marginRight="10dp"
android:textColor="#ffffff"
android:id="@+id/toolbar_title" />
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabIndicatorColor="@color/cardview_dark_background"
app:tabSelectedTextColor="@color/cardview_dark_background"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/floating_btn_heart"
android:layout_gravity="bottom|right"
android:background="@color/colorPrimary"
android:layout_margin="20dp"
android:id="@+id/fav_btn_heart"
/>
</android.support.design.widget.CoordinatorLayout>
最佳答案
尝试添加以下代码:
listView.setOnTouchListener(new ListView.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
events.
v.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
events.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
v.onTouchEvent(event);
return true;
}
});
说明,Android 对嵌套滚动没有很好的支持,因此您需要禁用“父”触摸管理并将事件传递给您的 View 。
希望这有帮助
关于android - fragment 中的 ListView 不滚动,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37889515/