2015-10-05 128 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

謝謝,我已經嘗試過了,並且在設計視圖中看起來完全像這樣,但它不起作用。 我在問題中添加了問題的圖片。 –