当我点击上面显示的项目时,我想要这种类型的标签,任何人都可以提供片段或链接.. 如果可以的话,请告诉我如何做到这一点.. 如图所示,红色矩形是那个或库的任何android工具.... 或直接我可以通过代码吗?
提前致谢
belove is image
...
答案 0 :(得分:3)
可以通过您的代码
在该线性布局中,您必须动态插入另一个布局,其中图像视图代表“>”和用于reprsent目录的文本视图 布局看起来像那种
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/arrow_image"
android:text="Storage"/>
<ImageView
android:id="@+id/arrow_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow"
android:layout_alignParentLeft="true"
android:layout_alignBottom="@id/textView"
android:layout_alignTop="@id/textView"/>
</RelativeLayout>
在动态添加View时,您只需要维护为clickevents添加的视图列表,这样您就可以直接切换到目录。当您移回上一个目录时,只需从视图列表中删除该视图即可!你做到了。
,您的代码段将如下所示
final ArrayList<View> myDynamicView = new ArrayList<>();
//it would be your list item click
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
View dynamicView = inflater.inflate(R.layout.field_layout, null);
dynamicView.setId(unique_id); // create a unique id to refer a view
TextView directoryName = (TextView) view.findViewById(R.id.textView);
directoryName.setText(your_directory_name);//Either fetch it through your array or by extracting the view
myDynamicView.add(myDynamicView.size(), dynamicView);
linearLayout.addView(dynamicView);
dynamicView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i= 0; i < myDynamicView.size(); i++){
//When view is clicked match the view id from the view list
if(myDynamicView.get(i).getId() == v.getId()){
//when view id got matched it means remove those view which are ahead of it
//from the list as well as from the linear layout
for(int j = i; j < myDynamicView.size(); j++){
View view = myDynamicView.get(j);
linearLayout.removeView(view);
myDynamicView.remove(j);
}
//update list
}
}
}
});
//update your list
}
});
答案 1 :(得分:3)
我为你做了一个简单的项目。你应该把它变得更漂亮,因为我并没有专注于那个,只是在代码上。
首先将这些值添加到 color.xml
<resources>
<color name="buttonGrey">#7A7A7A</color>
<color name="layoutHolderStartColor">#F7F7F7</color>
<color name="layoutHolderEndColor">#E1E1E1</color>
</resources>
接下来为按钮持有人创建一些背景并将其命名为 gradient_button_holder.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="@color/layoutHolderStartColor"
android:endColor="@color/layoutHolderEndColor"
android:angle="270"
/>
<corners android:radius="3dp" />
</shape>
现在创建 activity_main.xml
注意:我正在使用一些图片,从底部下载整个项目并将其取出
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:id="@+id/pathHolder"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_alignParentTop="true"
android:background="@drawable/gradient_button_holder"
android:gravity="center_vertical">
<Button
android:id="@+id/btnAdd"
android:layout_width="29dp"
android:layout_height="29dp"
android:layout_alignParentRight="true"
android:layout_marginBottom="3dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_marginTop="3dp"
android:background="@color/buttonGrey"
android:gravity="center"
android:onClick="onBtnAdd"
android:text="+"
android:textSize="15sp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:src="@drawable/seperator"
android:visibility="gone"/>
<HorizontalScrollView
android:id="@+id/btnScrollView"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/btnAdd">
<LinearLayout
android:id="@+id/btnFolderHolder"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
</RelativeLayout>
接下来创建 Utils 类
import android.annotation.SuppressLint;
import android.os.Build;
import android.view.View;
import java.util.concurrent.atomic.AtomicInteger;
public class Utils {
private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
/**
* Generate a value suitable for use in setId(int}.
* This value will not collide with ID values generated at build time by aapt for R.id.
*
* @return a generated ID value
*/
private static int generateViewId() {
for (; ; ) {
final int result = sNextGeneratedId.get();
// aapt-generated IDs have the high byte nonzero; clamp to the range under that.
int newValue = result + 1;
if (newValue > 0x00FFFFFF) {
newValue = 1; // Roll over to 1, not 0.
}
if (sNextGeneratedId.compareAndSet(result, newValue)) {
return result;
}
}
}
@SuppressLint("NewApi")
public static int generateId() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
return generateViewId();
}
else {
return View.generateViewId();
}
}
}
最后是MainActivity
import android.graphics.Color;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import java.io.File;
public class MainActivity extends ActionBarActivity {
private LinearLayout btnHolder;
private HorizontalScrollView scroller; //parent folders
private ViewTreeObserver observer; //needed for the scroll to the end
private Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
makeButtons(Environment.getExternalStorageDirectory().getPath(), "Folder1", "Folder2", "Folder3");
}
private void makeButtons(String... values) {
StringBuilder sb = new StringBuilder(values.length * 2);
for (int i = 0; i < values.length - 1; ++i) {
sb.append(values[i]);
sb.append(File.separator);
addButton(values[i], sb.toString(), true);
}
sb.append(values[values.length - 1]);
addButton(values[values.length - 1], sb.toString(), false);
}
private void init() {
setWidgetConnections();
observer = scroller.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
// this will always scroll to the last folder (displayed on the
// right)
scroller.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
});
}
private void setWidgetConnections() {
btnHolder = (LinearLayout) findViewById(R.id.btnFolderHolder);
scroller = (HorizontalScrollView) findViewById(R.id.btnScrollView);
}
public void onBtnAdd(View v) {
}
private void addButton(final String text, final String path, boolean withImage) {
// Dynamic call to add buttons
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setId(Utils.generateId());
btn.setText(text);
btn.setTextColor(Color.BLACK);
btn.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
goToPath(path);
}
});
//btn.setBackgroundResource(R.drawable.gradient_button_holder);
btnHolder.addView(btn, params);
if (withImage) {
ImageView view = new ImageView(this);
view.setBackgroundResource(R.drawable.seperator2);
btnHolder.addView(view, params);
}
}
private void goToPath(String path) {
showToast(path);
}
private void showToast(String text) {
if (toast != null) {
toast.cancel();
}
toast = Toast.makeText(this, text, Toast.LENGTH_SHORT);
toast.show();
}
}
这是最终结果,并且还注意到它是可水平滚动的
您可以下载整个项目here
答案 2 :(得分:1)
这实际上是使用Creating Swipe Views完成并更新选项卡的分隔符,或者您甚至可以使用HorizontalScrollView
自定义实现,并在进入文件夹时添加子项并在返回时删除最后一个子项