2015-10-05 46 views
1

我正在学习android开发,并且卡在Fragments中。我已阅读教程指导手册和一些Android开发人员文档,因此下一步是为我的手机构建应用程序。Android ListFragment重叠

我正在尝试制作购物清单。当你启动应用程序时,会显示一个菜单,显示三个选项,有问题的是第三个。

这个想法是允许用户创建更新并从该列表中删除产品。

所以这是我迄今所做

product_crud.xml

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

    <EditText 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/name_txt" 
    android:hint="@string/product_name" 
    android:inputType="text" 
     android:layout_alignParentStart="true" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/btn_add" 
     android:paddingLeft="10px" 
     android:text="@string/add_product" 
     android:layout_below="@+id/name_txt" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentEnd="true" /> 

</RelativeLayout> 

<ListView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/android:list" 
    android:layout_below="@+id/btn_add" 
    android:layout_alignParentStart="true" /> 

</LinearLayout> 

crudrowlayout.xml

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

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/btn_delete" 
    android:text="@string/remove_product" 
    android:layout_gravity="right" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentEnd="true" 
    /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/text_name" 
    android:textSize="40px" 
    android:layout_alignTop="@+id/btn_delete" 
    android:layout_alignParentStart="true" 
    android:layout_alignBottom="@+id/btn_delete" /> 


</RelativeLayout> 

PM_List_Fragment

public class PM_List_Fragment extends ListFragment { 

ProductsDataSource products; 

@Override 
public void onActivityCreated(Bundle savedInstanceState){ 
    super.onActivityCreated(savedInstanceState); 
    products = new ProductsDataSource(getActivity()); 
    try { 
     products.open(); 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 
    CrudArrayAdapter crudArrayAdapter = new CrudArrayAdapter(getActivity(),products.getAllProducts()); 
    products.close(); 
    setListAdapter(crudArrayAdapter); 
} 
} 

适配器

public class CrudArrayAdapter extends ArrayAdapter<Product> { 

private final List<Product> list; 
private final Activity context; 

static class ViewHolder{ 
    protected TextView name; 
    protected Button delete; 
} 


public CrudArrayAdapter(Activity context,List<Product> list) { 
    super(context, R.layout.rowlayout,list); 
    this.list = list; 
    this.context = context; 
} 


@Override 
public View getView(int position,View convertView,ViewGroup parent){ 
    View view; 
    if(convertView == null){ 
     LayoutInflater inflater = context.getLayoutInflater(); 
     view = inflater.inflate(R.layout.crudrowlayout,null); 
     final ViewHolder viewHolder = new ViewHolder(); 
     viewHolder.name = (TextView)view.findViewById(R.id.text_name); 
     viewHolder.delete = (Button)view.findViewById(R.id.btn_delete); 
     view.setTag(viewHolder); 
    }else{ 
     view = convertView; 
    } 
    ViewHolder holder = (ViewHolder) view.getTag(); 
    holder.name.setText(list.get(position).getName()); 
    return view; 
} 


} 

和我活动

public class CrudProducts extends ListActivity { 

private String msg = "Android: "; 
private ProductsDataSource productsDataSource; 


@Override 
public void onCreate(Bundle savedInstanceState){ 

    super.onCreate(savedInstanceState); 
    // ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment); 
    setContentView(R.layout.product_crud); 

    PM_List_Fragment pm_list_fragment = new PM_List_Fragment(); 

    FragmentManager fragmentManager = getFragmentManager(); 

    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 

    fragmentTransaction.add(android.R.id.content,pm_list_fragment); 

    Button btn_add = (Button)this.findViewById(R.id.btn_add); 

    btn_add.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      //create a form or put some input text so we can update the db! 
      EditText editText = (EditText)findViewById(R.id.name_txt); 
      if(editText.getText().length() > 0 && !productsDataSource.exists(editText.getText().toString())){ 
       Product prod = productsDataSource.createProduct(editText.getText().toString()); 
      // adapter.add(prod); 

      }else{ 
      if(editText.getText().length() == 0) 
      { 
       Toast.makeText(getApplicationContext(),"Please insert a name",Toast.LENGTH_LONG).show(); 
      } 
      else{ 
       Toast.makeText(getApplicationContext(),"This product already exists",Toast.LENGTH_LONG).show(); 
       } 
      } 
      // adapter.notifyDataSetChanged(); 
     } 
    }); 

    fragmentTransaction.commit(); 
} 

} 

不用担心数据源或适配器,这里的问题是,ListViewEditText重叠和Button(deleteBtn)。 列表视图显示在EditTextButton之上。我试图以不同的方式改变布局,但没有一个能够工作。

我读过,你可以用一个正常的活动和一个ListView实现这个相同的功能,但我想学习如何做到这一点。

有些想法?

问题的图片:

Problem overlap

+0

请只发表相关的代码/布局。读者通过扫描多个文件找到相关文件是非常令人困惑的。 – Naveed

+0

嗨,你的问题是在片段管理器。尝试用'replace()'方法替换'add()'片段。像:'transaction.replace(R.id.fragment_container,newFragment); transaction.addToBackStack(null); //提交交易 transaction.commit(); '[http://developer.android.com/training/basics/fragments/fragment-ui.html] – EugenUngurean

回答

1

那么有几件事我注意到这可能会帮助你!首先,CrudProducts extends ListActivityPM_List_Fragment extends ListFragment。如果您的意图是要显示一个列表,则应仅依赖于ListFragment。您可以将CrudProducts简单地扩展为Activity

如果你这样做,你可以有下面的布局设置为CrudProducts使用setContentView(R.layout.crud_activity)

crud_activity.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" > 

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

</LinearLayout> 

其次,在你的片段PM_List_Fragment添加

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.product_crud, container, false); 
    return view; 
} 

第三,product_crud.xml还需要一些细化

你并不需要以下属性为您<ListView>ListView父母是不是RelativeLayout的

android:layout_below="@+id/btn_add" 
android:layout_alignParentStart="true" 

而且你不需要在你的RelativeLayout

android:layout_alignParentStart="true" 
android:layout_alignParentEnd="true" 
孩子的

反而试着让你的Buttonmatch_parent

<Button 
    android:id="@+id/btn_add" 
    android:layout_width="match_parent" 

也可以尝试为您Button平衡填充在dp

android:paddingLeft="10dp" 
android:paddingRight="10dp" 

以上应解决您的问题,并列出应该是可见的正确。

0

你的主要观点是LinearLayout中,尝试将其更改为RelativeLayout的。 你应该尝试一下这样的:

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

    <RelativeLayout 
     android:layout_alignParentTop="true" 
     android:id="@+id/buttonView" 
     android:layout_width="match_parent" 
     android:layout_height="116dp" 
     android:orientation="vertical"> 

     <EditText 
      android:id="@+id/name_txt" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentStart="true" 
      android:inputType="text" /> 

     <Button 
      android:id="@+id/btn_add" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentEnd="true" 
      android:layout_alignParentStart="true" 
      android:layout_below="@+id/name_txt" 
      android:paddingLeft="10px" 
      /> 

    </RelativeLayout> 

    <ListView 
     android:id="@+id/android:list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentStart="true" 
     android:layout_below="@+id/buttonView" /> 

</RelativeLayout> 

,这将导致在此:

enter image description here

+0

谢谢,我已经尝试过了,并且在设计视图中看起来完全像这样,但它不起作用。 我在问题中添加了问题的图片。 –